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?

+3


source to share


2 answers


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

.

+3


source


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(/.*)?"})

      

+2


source







All Articles