如何在ASP.NET Core路由规则中添加前导零?

时间:2018-08-16 17:41:52

标签: c# asp.net-core

ASP.NET Core 2.1 MVC应用程序具有以下规则:

routes.MapRoute(
    name: "blog_list_year_month",
    defaults: new { 
        controller = "Blog", 
        action = "Index", 
        year = (int?)null, 
        month = (int?)null
    },
    template: "blog/{year?}/{month?}");

对于2018年1月,规则将生成类似blog/2018/1的网址。

如何表达月份应始终为两位数?即该引擎宁愿生成网址blog/2018/01

1 个答案:

答案 0 :(得分:0)

您是否已经查看了两个月的正则表达式并将其设置为路线约束?

例如:

new {month = @"(0[1-9]|1[0-2])"}

这应该将您的参数限制为两位数。