Branislav Abadjimarinov's technical blog - Browsing MVC
-
ASP.NET MVC default route problem
If you are working with ASP.NET MVC you definitely have seen that in the startup project in Visual Studio in Global.asax file there is a default route added like this:
routes.MapRoute(
"Content", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);It is very useful to quickly show how the MVC works but in commercial projects I'll suggest not to use a default route for handling /{Controller}/{Action} requests in ASP.NET MVC. By doing that you can get a ton of these exceptions:
System.Web.HttpException: The controller for path '/MyCustomPath/' could not be found or it does not implement IController.
The reason for this is that when you make a request for non-existing path like /MyCustomPath/ the MVC Controller factory will try to instantiate a class inheriting IController and named "MyCustomPath" and when It doesn't find it an exception will be thrown.
If the non-existing url is in some static file like .css or .js or the html of the page you can get a lot of these exceptions in just a few seconds if the web server is under a heavy load.The solution is simple - just remove the default route and add explicitly routes with the controllers you actually use. By doing that you'll get a 404 without the exception.