写了一个Attribute路由Route("Home/{category?}/{subcategory?}/List")
想匹配以下规则/Home/C1/S1/List
, /Home/C1/List
, /Home/List
好像只能匹配到第一种情况,可选参数没起作用。
如若匹配以上怎么用一个路由规则搞定?
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[Route("Home/{category?}/{subcategory?}/List")]
public IActionResult List(Category? category = null, SubCategory? subcategory = null)
{
return Content(category.ToString() + "/" + subcategory.ToString());
}
[Route("Home/{code}/Detail")]
public IActionResult Detail(string code)
{
return Content(code);
}
}
public enum Category
{
C1,
C2,
C3,
C4
}
public enum SubCategory
{
S1,
S2,
S3,
S4
}
在stackoverflow的提问:asp.net mvc routing with multiple optional parameters did not work
素胚勾勒不出你