使用HttpClient使用AttributeRouting在URL中发送日期

本文关键字:使用 日期 URL HttpClient AttributeRouting | 更新日期: 2023-09-27 18:26:35

我在获取WebAPI接受的日期范围查询时遇到一些问题。据我所读到的一切所知,这应该是有效的,但我仍然得到了400 Bad Request的回复。

我的API路线如下:

    [System.Web.Http.HttpGet]
    [GET("range/{start:datetime}/{end:datetime}")]
    public HttpResponseMessage Get(DateTime start, DateTime end)

我正在使用AttributeRouting库,根据这个页面,我请求的URL应该可以。

我的请求URL如下:

http://localhost:51258/plots/range/2013-07-29T21:58:39/2013-08-05T21:58:39

我在控制器RoutePrefix("plots")上有这个设置,这是URL路由的plots位的来源。

如果我去掉DateTime对象的时间,一切都很好,但我需要经过的时间。

使用HttpClient使用AttributeRouting在URL中发送日期

经过大量阅读,似乎可以做我试图做的事情,但这需要放松许多有用的安全措施。由于有一个简单的解决方法,因此鉴于安全风险的增加,放松这些措施是没有意义的。

我在API上得到的错误是:

A potentially dangerous Request.Path value was detected from the client (:)

显然,这是用于分隔DateTime字符串的时间部分元素的冒号。因此,我做了以下更改。

我的Api操作方法现在看起来是这样的:

[System.Web.Http.HttpGet]
[GET("range?{startDate:datetime}&{endDate:datetime}")]
public HttpResponseMessage Get(DateTime startDate, DateTime endDate)

日期现在被定义为查询字符串的一部分,而不是路径本身的一部分。

为了处理查询字符串的创建,我还有以下扩展方法:

public static string ToQueryString(this NameValueCollection source, bool removeEmptyEntries)
{
    return source != null ? "?" + String.Join("&", source.AllKeys
        .Where(key => !removeEmptyEntries || source.GetValues(key).Any(value => !String.IsNullOrEmpty(value)))
        .SelectMany(key => source.GetValues(key)
            .Where(value => !removeEmptyEntries || !String.IsNullOrEmpty(value))
            .Select(value => String.Format("{0}={1}", HttpUtility.UrlEncode(key), value != null ? HttpUtility.UrlEncode(value) : string.Empty)))
        .ToArray())
        : string.Empty;
}

它在我的客户端代码中使用如下:

var queryStringParams = new NameValueCollection
    {
        {"startDate", start.ToString(_dateService.DefaultDateFormatStringWithTime)},
        {"endDate", end.ToString(_dateService.DefaultDateFormatStringWithTime)}
    };
var response = httpClient.GetAsync(ApiRootUrl + "plots/range" + queryStringParams.ToQueryString(true)).Result;

我的应用程序中的日期服务只是提供默认的日期格式字符串,并使用以下模式:

"yyyy-MM-ddTHH:mm:ss"

由此生成的完整URI看起来像:

http://localhost:51258/plots/range?startDate=2013-07-30T21%3A48%3A26&endDate=2013-08-06T21%3A48%3A26

希望这对将来的其他人有所帮助。

:是URL中的保留字符,但在查询字符串中是可以接受的。因此,如果它适用于您的路由,您可以简单地使用:

[System.Web.Http.HttpGet]
[GET("range")]
public HttpResponseMessage Get(DateTime start, DateTime end)

您的URL请求可以如下所示:

http://localhost:51258/plots/range?start=2013-07-29T21:58:39&end=2013-08-05T21:58:39