【发布时间】:2017-02-15 12:51:57
【问题描述】:
我有一个添加到 RouteConfig 的自定义路由类
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new CustomRouting());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
CustomRouting 类如下所示:
public class CustomRouting : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var requestUrl = httpContext.Request?.Url;
if (requestUrl != null && requestUrl.LocalPath.StartsWith("/custom"))
{
if (httpContext.Request?.HttpMethod != "GET")
{
// CustomRouting should handle GET requests only
return null;
}
// Custom rules
// ...
}
return null;
}
}
基本上,我想使用我的自定义规则处理转到/custom/* 路径的请求。
但是:不是“GET”的请求不应使用我的自定义规则进行处理。相反,我想删除路径开头的 /custom 段然后让 MVC 继续使用 RouteConfig 中配置的其余路由。
我怎样才能做到这一点?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-routing