在我的 ASP.NET MVC3 应用程序中,我尝试模拟“routes.IgnoreRoute("...")”
我创建 CustomMvcRouteHandler:
public class CustomMvcRouteHandler: MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// do something
....
return base.GetHttpHandler(requestContext);
}
}
在我的 Global.asax.cs 文件中,我有:
protected void Application_Start()
{
// ............
RegisterRoutes(RouteTable.Routes);
// ............
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("elmah.axd");
//routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).RouteHandler = new CustomMvcRouteHandler();
}
我怎样才能做到这一点 ?
最佳答案
我不完全确定您在问题中的意思,但我会尽力回答...
模拟 IgnoreRoute
您需要做的就是关联 StopRoutingHandler
的一个实例从你的路线。如果您使用内置的 ASP.NET“路由”类,那么您将执行以下操作:
routes.MapRoute(
"Ignore-This", // Route name
"ignore/{this}/{pattern}" // URL with parameters
).RouteHandler = new StopRoutingHandler();
任何与该模式匹配的内容都会导致路由系统立即停止处理更多路由。
如果您想编写自己的自定义路由(例如,派生自
RouteBase
的新路由类型),则来自其 GetRouteData
您需要返回 StopRoutingHandler
的方法.
关于asp.net - 如何在我的自定义 MvcRouteHandler 中模拟 IgnoreRoute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4994644/