I am currently working on a ASP.NET MVC 4 Web Application project that must adhere to the following design decisions:
- The main MVC application resides in the root of the solution.
- All administrator functionality resides in a separate area.
- Each external party (e.g. suppliers) has its own area.
- Each area, including the root, constitutes a well separated functional block. Functionality from one area may not be exposed to another area. This is to prevent unauthorized access of data.
- Each area, including the root, has its own RESTfull API (Web API).
All normal controllers in all areas, including the root, work as expected. However, some of my Web API controllers exhibit unexpected behaviour. For instance, having two Web API controllers with the same name but in different areas produces the following exception:
Multiple types were found that match the controller named ‘clients’. This can happen if the route that services this request (‘api/{controller}/{id}’) found multiple controllers defined with the same name but differing namespaces, which is not supported.
The request for ‘clients’ has found the following matching controllers: MvcApplication.Areas.Administration.Controllers.Api.ClientsController MvcApplication.Controllers.Api.ClientsController
This seems strange since I have distinct routes that should separate both. Here is my AreaRegistration for the Administration section:
public class AdministrationAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Administration";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapHttpRoute(
name: "Administration_DefaultApi",
routeTemplate: "Administration/api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
context.MapRoute(
"Administration_default",
"Administration/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
Moreover, I notice that I can access Area specific Web API’s while omitting the name of the area from the call.
What is going on here? How do I get my Web API Controllers to behave just like normal ASP.NET MVC controllers?
You may place WebApi controllers in different Api folders in different Areas, but ASP.NET MVC will treat as if they are all in the same place.
Fortunately, you can overcome this limitation by overriding a part of the ASP.NET MVC infrastructure. For more information about the limitation and the solution, please read my blog post 'ASP.NET MVC 4 RC: Getting WebApi and Areas to play nicely'. If you are only interested in the solution, read on:
Step 1. Make your routes Area aware
Add the following extension methods to your ASP.NET MVC application and make sure they are accessible from your AreaRegistration classes:
To use the new extension method, remove the
Routes
property from the call chain:Step 2. Make Web API controller selector Area aware
Add the following class to your ASP.NET MVC application and make sure it is accessible from the Global.asax
Override the
DefaultHttpControllerSelector
by adding the following line to theApplication_Start
method in the Global.asax.Congratulations, your Web API controllers will now respect the rules of your areas just like your normal MVC controllers do!
UPDATE: 6 september 2012
Several developers have contacted me about a scenario they encountered where the
DataTokens
property of the route variable isnull
. My implementation assumes that theDataTokens
property is always initialized and will not function properly if this property isnull
. This behavior is most likely caused by recent changes in the ASP.NET MVC framework and may be actually be a bug in the framework. I’ve updated my code to handle this scenario.