52

How do I mix Web API and ASP.Net MVC pages in one project?

For instance, I have model User. I would like, within the same project, to have an ApiController that would respond to all the HTTP verbs for managing the User entities, and at the same time have a Controller that would return the appropriate strongly-typed Views depending on the Action requested.

I can't name both controllers UserController. What is the best way around this? Should I name one UserApiController and the other UserController? Any other suggestions?

2
  • The obvious answer is to have two controllers named with different names. What are you looking to do? Commented May 20, 2012 at 22:28
  • I'm just looking for an elegant structure to allow me to do this in the same project. I guess a separate namespace is a good idea. Commented May 21, 2012 at 1:54

3 Answers 3

53

You can put them in separate namespaces, e.g MyApp.Controllers.UsersController and MyApp.Controllers.WebAPI.UsersController.

This would let you expose similar URI routes in MVC and WebAPI, eg:

/users/1      << MVC view
/api/users/1  << Web API
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I like this better than different named classes.
What is the correct way to set up both default Routes in a combined project then?
@MikeWasson Just to clarify -- are you suggesting one controller class with two namespaces?
I don't think he is suggesting same file, but create a new folder under Controllers called "WebAPI", am I correct?
API controller and MVC controller have different interfaces, even though they both are descendants of ControllerBase.
16

I haven't changed the namespaces, the only thing that I had to do was register WebApi first, and then MVC route

//First register WebApi router
GlobalConfiguration.Configure(WebApiConfig.Register);
//and register Default MVC Route after
RouteConfig.RegisterRoutes(RouteTable.Routes);

and everything works great!

2 Comments

Man this wasted my 2 days, i tried creating API routes before any other routes but things needed in "GlobalConfiguration.Configure(WebApiConfig.Register)" should be called first. you saved me before it was too late.
The comments seem to be opposite to the code...comments say MVC then WebAPI, but code is WebApi then MVC
10

The WebApi implementation should be added as a separate Area in the MVC application. It is a natural fit. Doing this gives you the separate namespace that Mike Wasson recommended, plus it gives you a natural way to set up the /api routing. You get a separate model folder

Additionally, it is very specifically separated from the rest of the project. If requirements in the future are ever such that you need to separate the api implementation into a separate project, having the api implementation isolated to a separate area makes that breaking it out a lot easier.

4 Comments

It's not possible to keep web api inside of areas. Not without customization of the routing system.
@Vegar The customization of the routing system isn't hard, though: configuration.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional });
Does this matter when using attribute routing on all (API and MVC) controllers?
To answer my own question: This change makes a difference even if you use attribute routing on all MVC and API controllers, even though one wouldn't expect it to have any effect.

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.