区域内的Asp.Net MVC IgnoreRoute

时间:2010-09-12 19:25:25

标签: asp.net-mvc

如何忽略区域内的路线?

我在我的MVC应用程序中有验证码。它从GenericHanlder(.ashx)加载它的图像,所以如果我不使用区域它可以正常工作,如果我只是忽略来自该URL的路由。

即: 在Global.asax

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

问题是最近我将文件迁移到了另一个区域,并且修改了要忽略的路径的路径。现在它:

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

所以我尝试在Global.asax中的routes.IgnoreRoutes方法中更改该路径:

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

但它不再起作用。我已经尝试在RegisterRerea方法中忽略该路由,在AreaRegistrationFile中:

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

但这也是最重要的工作。

如何忽略某个区域的路线?

3 个答案:

答案 0 :(得分:4)

IgnoreRoute实际上只是对Add(new Route())的调用,Route的RouteHandler参数设置为新的StopRoutingHandler。 StopRoutingHandler告诉URL路由模块忽略路由并获取下一行内联HttpHandler。

您知道路线注册对声明的顺序很敏感。我的猜测是你在其他路线已经捕获之后宣布你的IgnoreRoute。

如果这些信息对您没有帮助,请发布您的路线注册的全部内容,因为它们将帮助我们给您答案。

此外,如果您使用http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx提供的源代码来调试路由,则可以更轻松地找到问题的原因。

答案 1 :(得分:3)

这适用于该区域的AreaRegistration类:

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 }
    );
}

这是在MVC4中,但也可以在旧版本中使用。

答案 2 :(得分:1)

对于MVC 5,也可能是4,RouteConfig现在是定义默认路由的地方。在默认路由映射之前,我有一些“routes.IgnoreRoute”调用,这里有一个例如:

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

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

现在,当我向网站添加“设置”区域时,导航到将触及该区域中的操作的URL,然后尝试导航到change.account,我在RouteConfig中的忽略路由调用不再阻止该调用从被送入路由引擎。

在阅读这篇文章并浏览其他地方后,我意识到我可以在我的SettingsAreaConfiguration.cs文件中实际上有相同的忽略路由调用,但稍有不同的是Routes作为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 }
    );
}
哇啦啦!问题解决了!!

现在我对IgnoreRoute进行了不少调用,因此我将所有那些在我的站点上标准化的调用复制到一个扩展RouteCollection的新扩展方法。

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

我可以在此方法中添加更多IgnoreRoute调用,现在我有一个存储所有这些调用的中心位置。

现在,我可以通过调用我的扩展方法替换“change.account”的特定IgnoreRoute调用。

因此,RouteConfig.cs文件中的调用将如下所示:

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 }
    );
}

SettingsAreaConfiguration.cs文件中的调用如下所示:

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

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

徐-weet! :O)

相关问题