how to add default parameters to attribute routes in asp.net mvc

后端 未结 2 1275
無奈伤痛
無奈伤痛 2021-01-19 04:47

I am trying to change this convention based route:

routes.MapRoute(
    \"MovieByReleaseDate\",
    \"movies/released/{year}/{month}\",
    new { controller          


        
2条回答
  •  粉色の甜心
    2021-01-19 05:22

    You can define route constriants in attributte routing to allow only some value

    [Route("movies/released/{year:regex(2015|2016)}/{month:regex(\\d{2}):range(1,12)}")]
        public ActionResult ByReleasedDate(int year, int month)
        {
            return Content($"year {year} and month {month}");
        }
    

    by using {year:regex(2015|2016)} it allows only 2015 or 2016 in year parameter

    by using {month:regex(\\d{2}):range(1,12)} it allows only 2 digit for month and range from 1 to 12

    Hope this helps

提交回复
热议问题