【问题标题】:How to pass DateTimeOffset values in ServiceStack如何在 ServiceStack 中传递 DateTimeOffset 值
【发布时间】:2013-06-21 20:19:58
【问题描述】:

我收到以下消息:

[Route("/devices/{DeviceId}/punches/{EmployeeId}/{DateTime}", "GET,POST")]
public class Punch
{
    public string DeviceId { get; set; }
    public string EmployeeId { get; set; }
    public DateTimeOffset DateTime { get; set; }
    public string TimeZoneId { get; set; }
    public PunchKind Kind { get; set; }
}

我需要在 DateTime 部分传递一个完整的DateTimeOffset...

以下工作完美:

http://localhost:54146/devices/1001/punches/666/2012-04-01

但是当我尝试传递完整的日期时间偏移数据时,它失败并出现 HTTP 400 错误:错误请求。到目前为止,我已经尝试了以下相同的错误(即使是 URL 编码也没有帮助):

http://localhost:54146/devices/1001/punches/666/2012-04-01T20:59:00.0000000-03:00 http://localhost:54146/devices/1001/punches/666/2012-04-01 20:59:00.0000000-03:00 http://localhost:54146/devices/1001/punches/666/2012-04-0120:59:00.0000000-03:00 http://localhost:54146/devices/1001/punches/666/2012-04-01T20%3A59%3A00.0000000-03%3A00

并且其他排列都失败并出现相同的错误。

如何将 DateTimeOffset 作为 URL 的一部分传递?

【问题讨论】:

  • 尝试对日期时间部分进行编码。例如2012-04-01T20%3A59%3A00.0000000-03%3A00
  • 我已经这样做了...我将更新问题以表明我已经尝试过了...

标签: asp.net servicestack


【解决方案1】:

<,>,*,%,&,:\ 默认情况下被 ASP.NET 禁止在 URL 中使用。在您的情况下,冒号是罪魁祸首(无论是否编码都无关紧要)。您可以通过将以下内容添加到您的 Web.config 文件来修复它:

<system.web>
...
    <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,\" />
...
</system.web>

参考资料:

【讨论】:

  • 效果很好!尽管它不适用于 Visual Studio 开发服务器(现在谁在使用它?)。您必须在 IIS 或 IIS Express 下使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多