1

I'm trying to build an asp.net mvc 4 application.

I want the application to encompass both a HTML site and a restful api, e.g.

www.mysite.com/MyDetails/ www.mysiste.com/api/users/{userid}/Details/

In the above example I would use 2 controller classes.

  1. MyDetailsController which inherits from System.Web.Mvc.Controller
  2. DetailsController which inherits from System.Web.Http.ApiController

I've also added a simple 'Users Route' to the WebApiConfig:

routeTemplate: "api/users/{userid}/{controller}/{id}

In my early testing it appears as though the following scenarios are invalid:

www.mysite.com/api/users/12345/MyDetails/ www.mysite.com/Details/

Both of those return a 404.

This is definitely a good thing but what I'm trying to find out is why doesn't it work? Can I rely on it not working or is it just coincidence in my simple test?

I've read about people struggling to develop a single MVC app/project that encompasses both HTML and REST apis but the most common complaint seems to be you can't duplicate controller names and it still seems like you can't simply use a namespace to differentiate them.

In this example I've deliberately designed the class names to avoid any conflict so what other gotchas are waiting to trip me up?

Thanks,

Chris A

2 Answers 2

1

Check your routes file, should be Global.asax under RegisterRoutes. The MapRoute call should tell you everything you need to know for MVC routing. Keep in mind, the order of the routes is important: top routes take priority over the bottom. Web API uses the WebApiConfig class and MapHttpRoute call to configure routes.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess the bit I don't understand is how come MapRoute doesn't resolve a System.Web.Http.ApiController and MapHttpRoute doesn't resolve a System.Web.Mvc.Controller? I guess it just doesn't but I'm trying to find out if that's deterministic behavior or not?
It is the way the framework is built. It is called the request pipeline and MVC has a separate pipeline from Web API.
Ah, so the bit I didn't realise is there's separate request pipelines for an MVC site and the Web API - seems weird seeing as they're both in the same project/application. Thanks
You could add aspx pages to the same project - would you then expect aspx+mvc+api to all use the same pipeline? It's strange because they're both modern(ish) web technologies developer at (roughly) the same time.
0

Please ensure you have put a (MVC) route on top of the action you wish to hit of your controller, default action being index.

    [System.Web.Mvc.Route("Help")]
    public ActionResult Index()
    {
        ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
        return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
    }

Code above will hit this action method (provided your controller is registered and derived either from apiController or Controller) in the following way: http://localhost:54541/help inside your IISExpress.

To register please do the following:

In "global.asax.cs", you’ll need to add:

AreaRegistration.RegisterAllAreas();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.