18

I've just started to use the VS 2012 RC, and I'm creating an ASP.NET MVC 4 web application in which I plan to provide both an HTML-based user interface and a WebApi-based programming interface.

For my HTML website, I have a controller and view for each of my models (MVC!), and the routing works "by convention" so that, for example, the URL /client hooks up to my ClientController. My ClientController derives from Controller.

For my API, I will create new controllers that derive from ApiController. I naturally want my API URLs to be similar to my HTML URLs, so I'd like the client info to be available at /api/client. However, with the by-convention routing, that would suggest that I need an ApiController named ClientController. And I already have a ClientController class.

How do I deal with this? Do I need custom routing? Do I put the API classes in different namespace so that I can give them the same name?

Update: this question seems to suggest that a different namespace for my API controllers is all I need: Mix web api controllers and site controllers

Community
  • 1
  • 1
Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • Have you looked at [MVC areas](http://msdn.microsoft.com/en-us/library/ee671793.aspx)? – Gordon Leigh Jul 19 '12 at 16:51
  • @gordonml: Thanks - I just did, and it seems to be sotra, kinda useful. However I can't help feeling that it's a little overblown for what I need to achieve. – Gary McGill Jul 20 '12 at 09:03
  • Does IsAjaxRequest work for your needs? http://stackoverflow.com/questions/1681563/detecting-isajaxrequest-with-asp-net-mvc-and-jquery-form-plugin-file-upload – kenny Jul 22 '12 at 14:04
  • @kenny: no, I'm trying not to "bend" stuff by writing code - I'm just trying to establish the correct way to do it "by convention", which seems to be to use a separate namespace for my API controllers (see Update). – Gary McGill Jul 22 '12 at 14:30

3 Answers3

12

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.

Gary McGill
  • 26,400
  • 25
  • 118
  • 202
2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Err... thanks, but that looks like the polar opposite of what I'm trying to acheive, in that I'm trying to "do it the MVC4 way" rather than bend it to do something that it wasn't designed for. – Gary McGill Jul 19 '12 at 16:25
  • Besides which, my two URLs will do completely different things. My normal URL presents a user interface to do all sorts of stuff. My API URL simply provides access to the data. So it's not really a question of returning "the same content" in different formats. – Gary McGill Jul 19 '12 at 16:27
  • 1
    Well in this case the `/api` prefix shouldn't bother you as it performs something completely different from the rest of the application. It allows you to clearly separate the API part from the website. – Darin Dimitrov Jul 19 '12 at 16:28
  • And also, the URLs are different anyway, since one is prefixed with "/api/". I *do* expect to have two different controllers for these two purposes - I'm just not clear about how to get the routing hooked up without resorting to wiring it up myself. – Gary McGill Jul 19 '12 at 16:28
  • 1
    I think our comments "crossed". I agree that the "/api/" bit isn't a problem. My problem is that I appear to need two different controllers with the same name. I don't know how best to avoid a naming clash, nor how the framework is going to distinguish between them and pick the right one. – Gary McGill Jul 19 '12 at 16:31
1

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.

nikib3ro
  • 20,366
  • 24
  • 120
  • 181