Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a specific route in ASP.NET MVC routing

What Is the proper syntax to put in the RouteConfig.RegisterRoutes method in an ASP.NET MVC application if I want the app to ignore all urls that start with the word "score" like

http://myserver/score*.*

?

In other words, any url that starts with the text "score" I want my app to ignore.

I tried:

routes.IgnoreRoute("score*.*/{*pathInfo}");

I also tried several other combinations of this syntax but can't quite get it right.

Here is what I have so far in my RouteConfig. It's pretty much the standard stuff.

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Order", action = "Add", id = UrlParameter.Optional }
        );
    }
}
like image 718
Ray Avatar asked Sep 27 '22 03:09

Ray


1 Answers

You should place your score*.* as a regEx expression in IgnoreRoute:

routes.IgnoreRoute("{score}/{*pathInfo}", new { score = @"score*.*" });

For more general answers, you can use this pattern to ignore routes that you want:

Routes.IgnoreRoute("{*foo*}", new { foo = @"someregextoignorewhatyouwant"});

So for score.js and score.txt route you will add regEx that filters that routes.

like image 168
freshbm Avatar answered Nov 14 '22 10:11

freshbm