【发布时间】:2013-08-11 21:32:03
【问题描述】:
(免责声明:MVC 新手)
我正在尝试设置一个网站产品列表,其中包含允许用户按产品文件夹/产品名称和产品文件夹/类别/产品名称请求页面的类别。这可能吗?
我最接近的是将以下内容添加到 RouteConfig.cs RegisterRoutes 方法中,并将 ProductNameOne.cshtml 放在 Views\Products 解决方案文件夹中。这导致能够看到“.../Products/Index”和“.../Products/ProductNameOne”页面,但“.../Products/CategoryOne/ProductNameOne”返回 404
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "Products", url: "Products/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id =
UrlParameter.Optional });
routes.MapRoute(name: "CategoryOneProducts", url: "Products/CategoryOne/{action}/{id}",
defaults: new { controller = "Products", action = "ProductNameOne", id =
UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
【问题讨论】:
-
您需要先按最具体的路线排列路线。
-
罗伯特的评论就是答案。与 CategoryOneProducts 交换产品路线
-
现在我不太确定这是否可能,stackoverflow.com/a/6226639/832705 问题答案指出您不能期望 MVC 处理能够区分控制器和操作
-
应该工作...
-
@stackuser83 - 对该帖子的评论是关于“/foo” “/bar” 在没有额外限制的情况下无法区分(即,如果您只有 2 条路线
"{foo}"和"{bar}")。
标签: c# asp.net-mvc asp.net-mvc-4 visual-studio-2012