如何忽略区域内的路由?
我有验证码在我的MVC application.It加载它的图像从一个GenericHanlder(.ashx),所以如果我不使用区域,它可以很好地工作,如果我只是忽略来自该application.It的路由。
即:在Global.asax中
routes.IgnoreRoute(Counts/{filename}.{ashx}/{*pathinfo});//Counts is the controller name without an area问题是,最近我将文件迁移到了另一个区域,而要忽略的路由的路径是modified.Now its:
Admin/Acounts/Users/captcha.ashx?guid=3565465689789hfghfdf所以我尝试在Global.asax中更改routes.IgnoreRoutes方法中的路径:
routes.IgnoreRoute(Admin/Acounts/Users/{filename}.ashx/{*pathinfo});但它已经不起作用了,在RegisterArea方法中,在AreaRegistrationFile中,尝试忽略该路由:
context.Routes.IgnoreRoute("Admin/Accounts/Users/{filename}.ashx/{*pathinfo}");但这也是可行的。
有什么办法可以忽略某个区域的路由吗?
发布于 2011-02-13 16:47:42
IgnoreRoute实际上只需调用Add(new Route()),将路由的RouteHandler参数设置为新的StopRoutingHandler。StopRoutingHandler告诉URL路由模块忽略该路由,并获取下一个内联HttpHandler。
您知道,路由注册对声明的顺序很敏感。我的猜测是,您是在其他路由已经捕获IgnoreRoute之后声明它的。
如果这些信息对您没有帮助,请发布您的路线注册的完整内容,因为它们将帮助我们为您提供答案。
此外,如果您使用http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx提供的源代码来调试路由,那么将更容易找到问题的原因。
发布于 2013-02-10 03:45:34
在这个区域的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中实现的,但也可以在旧版本中运行。
发布于 2014-04-18 00:12:13
对于MVC5,可能也是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 }
);现在,当我在站点中添加一个设置区域,导航到一个将点击该区域中的操作的网址,然后尝试导航到change.account时,我在RouteConfig中的忽略路由调用不再阻止该调用被馈送到路由引擎。
在阅读了这篇文章并在其他地方冲浪后,我意识到我的SettingsAreaConfiguration.cs文件中基本上可以有相同的忽略路由调用,只是路由作为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 }
);
}Suh-weet!:O)
https://stackoverflow.com/questions/3696224
复制相似问题