Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net WebAPI area support

I am trying to add some WebAPI support to my asp.net 4 RC site, and wish to put it into an area. I have seen that someone managed to get this running on the beta (here) , but to be honest, have no idea what I am doing in the RC.

Has anybody managed to add area support to the RC of WebAPI?

like image 497
Jon Bates Avatar asked Jun 17 '12 19:06

Jon Bates


People also ask

What is areas in Web API?

Areas are used for the management of the project. They are used in large projects. We create more than one area in a project. In this article, you will see the creation of areas.

Does .NET Web API provides routing support?

Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources.

Which of the following protocols does Web API support?

Web APIs are APIs that can be accessed using the HTTP protocol.


2 Answers

You can put Api controllers in any folder you like, you don't need to create an Area like you did for MVC. What I usually do is create a subfolder 'Api' inside 'Controllers' folder of MVC site. Just register routes for your Api controllers and it will work.

like image 57
frennky Avatar answered Sep 22 '22 12:09

frennky


Supporting areas in the web API URLs

You simply have to register the Web API route.

But this route must be registered before the other routes. If not, the requests to the web API will be mistakenly handled as if they were an MVC action.

When you're using areas, you must take into account that the registration for the areas routes is usually done before the registration of the non-area routes. I.e, in Global.asax Application_Start you have these lines of code, in this order:

AreaRegistration.RegisterAllAreas(); // ... RouteConfig.RegisterRoutes(RouteTable.Routes); 

The first method call will invoke the RegisterArea method of every found xxxAreaRegistration class. And, inside this configuration you'll usually have something like this:

context.MapRoute(     "AreaName_default",     "AreaName/{controller}/{action}/{id}",     new { action = "Index", id = UrlParameter.Optional } ); 

If you want to have Web API controllers inside this area, you must add the Web API route before this one, like so:

context.Routes.MapHttpRoute("AreaName_WebApiRoute",     "AreaName/Api/{controller}/{id}",     new { id = RouteParameter.Optional }); 

In this case, I'm assuming that you want to invoke your API controllers using an url like this: /AreaName/Api/ControllerName/Id.

I usually store the Web API controllers inside an Api folder inside the area folder, but it doesn't matter where you put them. Take into account that the API controllers will be found wherever they are and, if you use the same name for them in different areas, you'll get conflicts: they're found by the class name, not by the fully qualified (namespaced) name.

In few words: there's no real support for areas, but you can include them in their own folders inside the areas, and make them available on URL's which look like the ones of MVC controllers inside areas.

Supporting route namespaces

What if you want to have Web API controllers with the same name in different areas? If you really want to give real support for areas to Web API controllers, you have to implement and register a custom IHttpControllerSelector.

You have a good explanation, and a sample implementation here: ASP.NET Web API: Using Namespaces to Version Web APIs

This sample uses namespaces for versioning, but the code can be slightly modified to support areas namespaces.

like image 26
JotaBe Avatar answered Sep 18 '22 12:09

JotaBe