如何传递日期时间参数? [英] How to pass a datetime parameter?

查看:19
本文介绍了如何传递日期时间参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 UTC 日期传递给 Web API?

How to pass UTC dates to Web API?

传递 2010-01-01 工作正常,但是当我传递一个 UTC 日期时,例如 2014-12-31T22:00:00.000Z(带有时间组件),我收到一个 HTTP 404 响应.所以

Passing 2010-01-01 works fine, but when I pass a UTC date such as 2014-12-31T22:00:00.000Z (with a time component), I get a HTTP 404 response. So

http://domain/api/controller/action/2012-12-31T22:00:00.000Z

产生 404 错误响应,同时

yields a 404 error response, while

http://domain/api/controller/action/2012-12-31

工作正常.

然后如何将 UTC 日期传递给 Web API - 或者至少指定日期时间?

How to pass UTC dates to Web API then - or at least specify date and time?

推荐答案

问题是双重的:

默认情况下,IIS 将所有带有点的 URI 视为静态资源,尝试返回它并完全跳过进一步处理(通过 Web API).这是在您的 Web.config 部分的 system.webServer.handlers 中配置的:默认处理程序处理 path="*.".你不会在这个 path 属性中找到太多关于奇怪语法的文档(正则表达式会更有意义),但这显然意味着任何不包含点的东西"(以及任何来自下面第 2 点的字符).因此名称中的Extensionless"ExtensionlessUrlHandler-Integrated-4.0.

By default, IIS treats all URI's with a dot in them as static resource, tries to return it and skip further processing (by Web API) altogether. This is configured in your Web.config in the section system.webServer.handlers: the default handler handles path="*.". You won't find much documentation regarding the strange syntax in this path attribute (regex would have made more sense), but what this apparently means is "anything that doesn't contain a dot" (and any character from point 2 below). Hence the 'Extensionless' in the name ExtensionlessUrlHandler-Integrated-4.0.

在我看来,按照正确性"的顺序可以有多种解决方案:

Multiple solutions are possible, in my opinion in the order of 'correctness':

  • 专门为必须允许点的路由添加新的处理程序.确保在默认情况下之前添加它.为此,请确保先删除默认处理程序,然后在您的之后重新添加.
  • path="*." 属性更改为 path="*".然后它将捕获所有内容.请注意,从那时起,您的 web api 将不再将带有点的来电解释为静态资源!如果您在 Web api 上托管静态资源,则不建议这样做!
  • 将以下内容添加到您的 Web.config 以无条件地处理所有请求:在 下:
  • Add a new handler specifically for the routes that must allow a dot. Be sure to add it before the default. To do this, make sure you remove the default handler first, and add it back after yours.
  • Change the path="*." attribute to path="*". It will then catch everything. Note that from then on, your web api will no longer interpret incoming calls with dots as static resources! If you are hosting static resources on your web api, this is therefor not advised!
  • Add the following to your Web.config to unconditionally handle all requests: under <system.webserver>: <modules runAllManagedModulesForAllRequests="true">

更改上述内容后,默认情况下,您会收到以下错误:

After you've changed the above, by default, you'd get the following error:

从客户端 (:) 检测到潜在危险的 Request.Path 值.

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

您可以更改 Web.config 中预定义的不允许/无效字符.在 下,添加以下内容:.我已经从无效字符的标准列表中删除了 :.

You can change the predefined disallowed/invalid characters in your Web.config. Under <system.web>, add the following: <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,%,&amp;,*,,?" />. I've removed the : from the standard list of invalid characters.

虽然不是您问题的答案,但更安全、更简单的解决方案是更改请求,以便不需要所有这些.这可以通过两种方式完成:

Although not an answer to your question, a safer and easier solution would be to change the request so that all this is not required. This can be done in two ways:

  1. 将日期作为查询字符串参数传递,例如 ?date=2012-12-31T22:00:00.000Z.
  2. 从每个请求中去除 .000.您仍然需要允许 : 的(参见第 2 点).
  1. Pass the date as a query string parameter, like ?date=2012-12-31T22:00:00.000Z.
  2. Strip the .000 from every request. You'd still need to allow :'s (cfr point 2).

这篇关于如何传递日期时间参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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