我使用MVC 5.2.3 4.5.1,并希望这些URLMVC路由可选字符串参数和丑陋的网址
- /耐克/鞋
- /耐克/鞋/运行
- /耐克/衬衫/ 2002342345
- /耐克/鞋/跑步/ 98765432234
我的控制器有:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, int? page, string subcategory = "")
,并给出/耐克/鞋/运行,但没有/耐克/鞋,但/产品?品牌=耐克&类别=鞋
当我添加
[HttpGet]
[Route("{brand}/{category}/{page:int?}")]
public ActionResult Index(string brand, string category, int? page)
/耐克/鞋/工程,但我也有/耐克/鞋/子类=运行
和我的个人信息的方法:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")]
public ActionResult Details(int productid)
给出:
当前请求是采用以下的操作方法之间暧昧: System.Web.Mvc.ActionResult指数(System.String,System.String,System.Nullable`1 [System.Int32],系统.String)on shopmvc.Controllers.ProductsController System.Web.Mvc.ActionResult索引(shopmvc.ViewModels.ProductsViewModel)类型shopmvc.Controllers.ProductsController System.Web.Mvc.ActionResult类型shopmvc.Controllers上的详细信息(Int32) .ProductsController
因此,我的产品链接和类别/子类别路线有问题。或与我试图达到它的方式:
<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>
我的子类是一个可选参数,但似乎不起作用。 asteriks在url模板'* subcategory'中做了什么? –
这可以防止模板进一步解析URL的其他部分(简要解释),可以找到更多[链接](https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29 .aspx) – Razieltje
因此,您建议使用https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29.aspx#handling_a_variable_number_of_segments_in_a_url_pattern,然后手动解析单个httpget方法中的字符串。如果它以大于10000的重定向操作结束,并且在'/'类别上进行细化和分割等等,我不知道这是否是最好的方法。尽管谢谢你的建议。也许其他用户可以证明这种方法? –