MR.ABC
MR.ABC

Reputation: 4862

MVC Routing match all routes except resources

I've a angularJS application. I've created a controller/action that returns a rendered index.cshtml on any request. This is my only mvc4 controller. I use the webapi for all other behavior. The client-side should only handling the 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 request like this ?

Upvotes: 4

Views: 1499

Answers (2)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

You can try something like this, then routing module ignores requests for any .html and .ico files :-

routes.IgnoreRoute("{*allhtml}", new {allhtml=@".*\.html(/.*)?"});
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"})

Upvotes: 2

Daniel J.G.
Daniel J.G.

Reputation: 34992

You could use IgnoreRoute so the routing module ignores requests for those resources, for example:

routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{templateName}.html");

These have to be added before your current Default route.

Upvotes: 4

Related Questions