slug
是内容路径的其余部分(托管在正在运行的应用程序外部 - 该路由将代理请求服务器端)。我尝试向操作添加属性,并尝试在
RouteConfig
中创建路线。每当我请求真实内容路径时;比如
{domain}/gb/en/content/assets/head/icons/icon.png
我得到了 404;如果我转到 /slug
,我就会点击操作方法。
[Route("{country}/{language}/content/{*slug?}")]
public ActionResult Content(string slug = null)
{
// ... do something
}
或
routes.MapRoute(
"Content",
"{country}/{language}/content/{*slug?}",
new
{
controller = "Cloud",
action = "Content",
slug = UrlParameter.Optional
}
);
Route
属性与正则表达式一起使用,如下所示:
[Route("{country}/{language}/content/{slug:regex(.*slug)?}")]
public new ActionResult Content(string slug = null)
{
...
}
另外,使用 routes.MapMvcAttributeRoutes();
属性时,需要在
RegisterRoutes()
中调用
Route
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}