Asp.net mvc Asp.Net MVC区域内的IgnoreRoute
如何忽略区域内的路线 我的MVC应用程序中有captcha。它从GenericHander(.ashx)加载它的图像,所以如果我不使用区域,它可以正常工作,如果我忽略来自该URL的路由 i、 e: 在Global.asax中Asp.net mvc Asp.Net MVC区域内的IgnoreRoute,asp.net-mvc,Asp.net Mvc,如何忽略区域内的路线 我的MVC应用程序中有captcha。它从GenericHander(.ashx)加载它的图像,所以如果我不使用区域,它可以正常工作,如果我忽略来自该URL的路由 i、 e: 在Global.asax中 routes.IgnoreRoute(Counts/{filename}.{ashx}/{*pathinfo});//Counts is the controller name without an area 问题是,最近我将文件迁移到了另一个区域,要忽略的路由路径被修改
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});
但是它不再起作用了。我已经尝试在RegisterArea方法的AreaRegistrationFile中忽略该路由:
context.Routes.IgnoreRoute("Admin/Accounts/Users/{filename}.ashx/{*pathinfo}");
但这也不起作用
知道如何忽略某个区域的Routes吗?IgnoreRoute实际上只是一个要添加的调用(new Route()),其中Route的RouteHandler参数设置为一个新的StopRoutingHandler。StopRoutingHandler告诉URL路由模块忽略路由并获取下一个内联HttpHandler将是什么 你知道路线登记对申报顺序很敏感。我的猜测是,在其他路线已经捕捉到你的IgnoreRoute后,你正在宣布你的IgnoreRoute 如果这些信息对您没有帮助,请发布路线注册的全部内容,因为它们将帮助我们为您提供答案
此外,如果您使用中提供的源代码调试路由,将更容易找到问题的原因。这对我来说适用于该区域的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中,但在旧版本中也可以使用。对于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 }
);
现在,当我在站点中添加了一个设置区域,导航到一个会在该区域中点击某个操作的URL,然后尝试导航到change.account时,RouteConfig中的ignore route调用不再阻止该调用进入路由引擎
在阅读了这篇文章并浏览了其他地方之后,我意识到我的setingsareaconfiguration.cs文件中基本上可以有相同的ignore route调用,只是路由作为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 }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoutesForCommon();
context.MapRoute(
"Settings_default",
"Settings/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
SettingsAreaConfiguration.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 }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoutesForCommon();
context.MapRoute(
"Settings_default",
"Settings/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
苏威!:O) IgnoreRoute()用于Global.asax RegisterRoutes方法,它并不知道您是否忽略某个区域、控制器或某个文件夹的路由。所有它看到的是一个网址。我有同样的问题,像你一样,我有两个地区,它不会忽略地区管理员的路线!!!!谢谢。这正是我所需要的,因为直接忽略RouteConfig中的路由对我不起作用,而且
上下文。IgnoreRoute()
在RegisterArea
方法中不是有效的方法调用。