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?
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.
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.
Web APIs are APIs that can be accessed using the HTTP protocol.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With