我有一个控制器操作,如下所示:
public ActionResult Content(string slug)
{
var content = contentRepository.GetBySlug(slug);
return View(content);
}
我希望将此类网址路由到我的操作:
http://localhost/slug-of-my-content http://localhost/slug-of-my-content
这是我的 RegisterRoutes 方法:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Page", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "GetContent",
url: "{slug}",
defaults: new { controller = "Page", action = "Content", slug = "" }
);
}
但它不起作用,我做错了什么?
Thanks,
1 将slug路由放在默认路由之上,如果没有,则永远不会走slug路由
2 你的slug不能为空,如果为空,则url将为http://本地主机/ http://localhost/它必须走默认路线
routes.MapRoute(
name: "slug",
url: "{slug}",
defaults: new { controller = "Home", action = "show" },
constraints: new{ slug=".+"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
我认为不要选择“Content”作为操作名称,因为基类中有一个 Content 方法
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)