Can I have both a Controller and an ApiController for the same thing? Can I have both a Controller and an ApiController for the same thing? asp.net asp.net

Can I have both a Controller and an ApiController for the same thing?


All it requires is for the controller classes to be in a different namespace, and all is well.

Using MVC areas would also work (as suggested in gordonml's comment), but this effectively puts the controllers in different namespaces, so it's a more formal way of achieving the same result.


You may take a look at the following blog post which illustrates how an Api controller could serve Razor views as well. Basically he uses the RazorEngine to parse the Razor view end serve it.


For anyone looking for step by step guidance on how to do this on WebApi project:

  1. Create two folders / namespaces, namely: ControllersApi and ControllersWeb
  2. Right click on ControllersWeb and go Add -> Controller and select MVC 5 Controller - Empty. This will add all other dependencies if you didn't have them in your WebApi project.
  3. Your RouteConfig will now register those classes that inherit from Controller base class. You'll likely need to add link to default Controller, by editing defaults to say: defaults: new { action = "Index", controller = "Home", id = UrlParameter.Optional }

That's it, you can now run site and use both API and Web controllers.