C#学习教程:路由属性中的正则表达式–RESTfulAPIASP.NETWebAPI我想创建RESTfulAPI,您可以在其中指定URL中的开始和结束日期以过滤结果。到目前为止我所做的是:[HttpGet][Route("date/{startDate:datetime:regex(\d{4}-\d{2}-\d{2})}/{*endDate:datetime:regex(\d{4}-\d{2}-\d{2})}")][Route("date/{startDate:datetime:regex(\d{4}/\d{2}/\d{2})}/{*endDate:datetime:regex(\d{4}/\d{2}/\d{2})}")]publicIEnumerableGetRecommendationByDate(DateTimestartDate,DateTime?endDate){var输出=db.Recommendations.Where(r=>r.IsPublished==true&&r.CreatedDate.CompareTo(startDate)>0&&r.CreatedDate.CompareTo(endDate.HasValue?endDate.Value:DateTime.Now)r.LastModified).ToList();返回输出;这不是我想要的,因为第二个参数应该可以为空。当我只传递开始日期时,我得到一个404。使用斜杠格式也不起作用。我究竟做错了什么?我认为*意味着参数可以为空...===EDIT===我要匹配的网址都是:https://localhost:post/api/recommendations/date/10/07/2013/1/08/2014-不工作https://localhost:post/api/recommendations/date/10-07-2013/1-08-2014-工作并且可以是第二个参数:https://localhost:post/api/recommendations/date/10/07/2013-不起作用https://localhost:post/api/recommendations/date/10-07-2013-不起作用对于可为空的第二个参数,编写路径模板对于[Route("api/recommendations/date/{startDate:datetime:regex(\d{2}-\d{2}-\d{4})}/{endDate:datetime:regex(\d{2}-\d{2}-\d{4})?}")]你应该为可空参数提供默认值publicIEnumerableGetRecommendationByDate(DateTimestartDate,DateTime?endDate=null)对于斜杠,斜杠用于分隔url段,这意味着除非编码,否则单个段不能包含斜杠。以上就是C#学习教程分享的全部内容:路由属性中的正则表达式-RESTfulAPIASP.NETWebAPI。如果对你有用,需要了解更多C#学习教程,希望大家多加关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
