正则表达式的路由属性 - 基于REST API的ASP.NET Web API [英] Regex in Route attribute - RESTful API ASP.NET Web API

查看:250
本文介绍了正则表达式的路由属性 - 基于REST API的ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在路由属性正规前pressions一个问题。我想创建RESTful API,在那里你可以指定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})}\")]
    公共IEnumerable的<推荐> GetRecommendationByDate(日期时间的startDate,日期时间?结束日期)
    {
        无功输出= db.Recommendations
            。凡(R = GT; r.IsPublished ==真放;&安培;
                        r.CreatedDate.CompareTo(的startDate)GT; 0安培;&安培;
                        r.CreatedDate.CompareTo(?endDate.HasValue endDate.Value:DateTime.Now)LT; 0)
            .OrderByDescending(R => r.LastModified)
            .ToList();        返回输出;
    }

它不工作,我多么希望,因为第二个参数应为空。当我通过只启动至今,我得到404还与斜线格式并不在所有的工作。我究竟做错了什么?我以为*表示该参数为空...

===编辑===

我的网址匹配均为:

https://开头本地主机:交/ API /推荐/日期/ 10/07 /一分之二千〇一十三/ 08/2014年 - 不工作

https://开头本地主机:交/ API /推荐/日/ 2013年10月7日/ 2014年1月8日 - 工程

和可空第二个参数:

https://开头本地主机:交/ API /推荐/日期/ 10/07/2013 - 不工作

https://开头本地主机:交/ API /推荐/日/ 2013年10月7日 - 不工作


解决方案

有关为空的第二个参数,写你的路线模板

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

和你应该为空的参数的默认值提供

 公开的IEnumerable&LT;推荐&GT; GetRecommendationByDate(日期时间的startDate,日期时间?结束日期= NULL)

有关斜线,斜线用来分隔URL段,这意味着单个段不能包含斜线,除非它们是连接codeD。

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. What I've done till now is:

    [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})}")]
    public IEnumerable<Recommendation> GetRecommendationByDate(DateTime startDate, DateTime? endDate) 
    {
        var output = db.Recommendations
            .Where(r => r.IsPublished == true &&
                        r.CreatedDate.CompareTo(startDate) > 0 &&
                        r.CreatedDate.CompareTo(endDate.HasValue ? endDate.Value : DateTime.Now) < 0)
            .OrderByDescending(r => r.LastModified)
            .ToList();

        return output;
    }

It doesn't work how I want, because second param should be nullable. When I pass only start date, I'm getting 404. Also format with slash doesn't work at all. What am I doing wrong? I thought * means that parameter is nullable...

===EDIT===

My URLs to match are both:

https:// localhost:post/api/recommendations/date/10/07/2013/1/08/2014 - doesn't work

https:// localhost:post/api/recommendations/date/10-07-2013/1-08-2014 - works

and with nullable second parameter:

https:// localhost:post/api/recommendations/date/10/07/2013 - doesn't work

https:// localhost:post/api/recommendations/date/10-07-2013 - doesn't work

解决方案

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.

这篇关于正则表达式的路由属性 - 基于REST API的ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆