0
我有我加入RouteConfigMVC自定義路由僅適用於GET請求
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中配置的其餘路由。
我該如何做到這一點?
好主意。謝謝! – Sandro