MVC routing matches all routes except resources
I have an angularJS application. I created a controller / action that returns the processed index.cshtml for any request. This is my only mvc4 controller. I am using webapi for any other behavior. The client side has to handle routes. Works well so far.
RouteExistingFiles = false;
routes.MapRoute(
name: "Default",
url: "{*url}",
defaults: new { controller = "Index", action = "Index" }
);
The problem is that this solution matches resources like localhost / favicon.ico, localhost / template.html. Can I prevent or ignore all requests like this?
You can use IgnoreRoute
to make the routing module ignore requests for these resources, for example:
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{templateName}.html");
They must be added before your current route Default
.
You can try something like this, then the routing module ignores requests for any .html and .ico files : -
routes.IgnoreRoute("{*allhtml}", new {allhtml=@".*\.html(/.*)?"});
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"})