猿问

asp.net mvc路由可选参数不能匹配

写了一个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

暮色呼如
浏览 1085回答 2
2回答

素胚勾勒不出你

一个路由应该是做不到,使用三个可以[Route("Home/List")] [Route("Home/{category?}/List")] [Route("Home/{category?}/{subcategory?}/List")]
随时随地看视频慕课网APP
我要回答