Regex in Route attribute - RESTful API ASP.NET Web API

后端 未结 1 810
无人及你
无人及你 2021-01-16 21:09

I\'ve got a problem with regular expressions in Route attribute. I\'d like to create RESTful API, where you can specify start date and end date in URL to filter results. Wha

相关标签:
1条回答
  • 2021-01-16 21:46

    For nullable second parameter, write your route template as

    [Route("api/recommendations/date/{startDate:datetime:regex(\\d{2}-\\d{2}-\\d{4})}/{endDate:datetime:regex(\\d{2}-\\d{2}-\\d{4})?}")]
    

    And you should provide with a default value for nullable parameter

    public IEnumerable<Recommendation> GetRecommendationByDate(DateTime startDate, DateTime? endDate = null)
    

    For slashes, slash is used to separate url segments, which means one single segment can't contain slash unless they are encoded.

    0 讨论(0)
提交回复
热议问题