我在4.5.1上使用mvc 5.2.3并想要这些网址
我的控制器有:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, int? page, string subcategory = "")
并给予/ Nike / Shoes / Running但没有/ Nike / Shoes但/ Products?brand = Nike& category = Shoes
当我添加
[HttpGet]
[Route("{brand}/{category}/{page:int?}")]
public ActionResult Index(string brand, string category, int? page)
/ Nike / Shoes / works,但我也有/ Nike / Shoes /?subcategory = Running
和我的详细信息方法:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")]
public ActionResult Details(int productid)
给出:
以下操作方法之间的当前请求不明确: 类型shopmvc.Controllers.ProductsController上的System.Web.Mvc.ActionResult索引(System.String,System.String,System.Nullable`1 [System.Int32],System.String) 类型shopmvc.Controllers.ProductsController上的System.Web.Mvc.ActionResult索引(shopmvc.ViewModels.ProductsViewModel) 类型shopmvc.Controllers.ProductsController上的System.Web.Mvc.ActionResult Details(Int32)
所以我的产品链接和类别/子类别路线有问题。或者按照我试图达到的方式:
<a href="@Url.RouteUrl("ProductDetails", new { brand = item.Brand, category = item.Category, subcategory = item.SubCategory, productid = item.ID })">test</a>
<a href="@Url.Action("Details", "Products", new { brand = item.Brand, category = item.Category, subcategory = item.SubCategory, productid = item.ID })">
<a href="@Url.Action("Index", "Products", new { brand = c.Brand, category = c.Category, subcategory = c.SubCategory })">@c.DisplayName</a>
答案 0 :(得分:2)
您是否尝试过Global.asax
中从最具体到最通用的路线?您的应用程序不知道检查路由的顺序,因此brand/category/subcategory/nnnnn
之类的内容可以是page nnnnn或product nnnnn。
您需要先检查此路线:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")]
public ActionResult Details(int productid)
然后这个:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, int? page, string subcategory = "")
或者,您可以修改路由,以便页面路由如下所示:
`brand/category/subcategory/p-n`
e.g。 /Nike/Shoes/Running/p-2
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{page:int?:max:8999}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, int? page, string subcategory = "")
答案 1 :(得分:1)
如何获得顶级通用级别,并从那里确定其余部分?
[HttpGet]
[Route("{brand}/{category}/{*subdivision}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, string subdivision)
然后确定您的细分的分割是否包含更深的路径,或产品ID,并根据该选择调用正确的方法