开发者

Asp.Net MVC IgnoreRoute inside an Area

How to ignore a route inside an area?

I have captcha in my MVC application.It loads its image from an GenericHanlder(.ashx),so if imnot using areas it works fine, if I just ignore routes from that URL.

i.e: In Global.asax

routes.IgnoreRoute(Counts/{filename}.{ashx}/{*pathinfo});//Counts is the controller name without an area

The problem is that recently I migrated the files to a different area and the path of the route to be ignored was modified.Now its:

Admin/Acounts/Users/captcha.ashx?guid=3565465689789hfghfdf

So i try to change that path in routes.IgnoreRoutes method in Glo开发者_高级运维bal.asax:

routes.IgnoreRoute(Admin/Acounts/Users/{filename}.ashx/{*pathinfo});

But it doesnt work anymore.Ive already try to ignore that route in RegisterArea method,in AreaRegistrationFile:

    context.Routes.IgnoreRoute("Admin/Accounts/Users/{filename}.ashx/{*pathinfo}");

But that also doenst work.

Any ideas how to ignore routesto an Area?


IgnoreRoute is really just a call to Add(new Route()), with the RouteHandler parameter of the Route set to be a new StopRoutingHandler. The StopRoutingHandler tells the URL routing module to ignore the route and get whatever the next-inline HttpHandler would be.

You know that route registration is sensitive to the order of declaration. My guess is that you are declaring your IgnoreRoute after some other route already catches it.

If this bit of info doesn't help you, please post the full contents of your route registration as they will help us to give you answers.

Also, if you use the source code provided at http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx for debugging routes, it will be much easier to find the cause of your problem.


This works for me in the AreaRegistration class of the area:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.Routes.Add(new Route("admin/{sub}/{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
    context.Routes.Add(new Route("admin/{resource}.axd/{*pathInfo}", new StopRoutingHandler()));

    context.MapRoute(
        "admin_default",
       "admin/{controller}/{action}/{id}",
        new { controller = "home", action = "Index", id = UrlParameter.Optional }
    );
}

This is in MVC4 but it may work in older versions as well.


For MVC 5, probably 4 as well, RouteConfig is now where the default route is defined. Before the default route mapping, I had a number of "routes.IgnoreRoute" calls, and here is one for example:

routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Now, when I added a Settings area to the site, navigated to a URL that would hit an action in the area, then tried to navigate to change.account, my ignore route call in the RouteConfig was no longer blocking that call from being fed into the Routing engine.

After reading this post and surfing other places, I realized I could essentially have the same ignore route call in my SettingsAreaConfiguration.cs file, with the slight difference that Routes lives as a property of the AreaRegistrationContext.

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.Routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });

    context.MapRoute(
        "Settings_default",
        "Settings/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

And wah-lah!! Problem solved!!

Now I had quite a few calls to IgnoreRoute, so I copied all those calls that were standard across my site to a new Extension Method that extends RouteCollection.

public static class RouteCollectionExtensions
{
    public static void IgnoreRoutesCommon(this RouteCollection routes)
    {
        routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
    }
}

I can add more IgnoreRoute calls inside this method, now that I have a central place to store all of these calls.

Now, I can replace the specific IgnoreRoute call for "change.account" with a call to my extension method.

So the call in the RouteConfig.cs file will look like:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoutesForCommon();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

And the call in the SettingsAreaConfiguration.cs file will look like:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.Routes.IgnoreRoutesForCommon();

    context.MapRoute(
        "Settings_default",
        "Settings/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

Suh-weet! :O)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜