7/14/18

ASP.NET MVC Routing Error Multiple Controller Types on Azure Virtual Directory

In a previous article, I wrote about hosting an ASP.NET MVC app on a virtual directory on Azure.  You can find the article in the link below.  The main goal of that article was to show how to hosts other sites within the same Azure Web app.



There are cases when the multiple sites have similar routes and controllers, and the application find this ambiguous and does not know how to handle a request showing this error:



[InvalidOperationException: Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers:
Admin.Controllers.HomeController
Main.Controllers.HomeController]
               
This error is somewhat odd because a deployment to a virtual directory should isolate the routes per application and there should not be this ambiguity.

Review Deployment and Configuration

When facing such error, we need to check the following possible problems:

1) When doing the deployment, we need to make sure the destination URL includes the virtual directory folder
For example:   www.mysite.com/admin    where admin is the virtual directory folder.

2) Make sure the virtual directory root folder is not the same as the main site
This is a common mistake. When doing the configuration for a virtual directory we must make sure to set the correct physical path as shown next:


The image shows how the main application and virtual path use different physical paths. If both are set to the same physical path, then the routes will be processed from both apps with the same route but different controller types which causes the ambiguity.

Before doing the deployment to Azure make sure to test this configuration on your development environment. For those using Visual Studio with IIS Express, you may find this article useful:
I hope this is helpful.

Originally published by ozkary.com

0 comments :

Post a Comment