【发布时间】:2014-10-23 14:12:26
【问题描述】:
我正在编写一个 ASP.Net Web Api (2.2)。这是一个报告系统(漂亮的图表和东西)
我想通过上一个/下一个按钮提供数据的日/周/月视图。
我的 URI 路径如下所示:
config.Routes.MapHttpRoute(
name: "Api UriPathExtension Report DateCategory ISO 8601",
routeTemplate: "api/{controller}/{dateCategory}/{iso8601date}.{ext}",
defaults: new
{
ext = RouteParameter.Optional
}
);
我想获取我的 iso8601date 并根据 dateCategory 参数中的值(日/周/月)进行处理。
/// <param name="dateCategory">string {Day/Week/Month}</param>
/// <param name="iso8601date">The iso8601 compliant
/// date(2014-10-22),
/// week(2014-W43),
/// or month(2014-10)</param>
DateTime date = DateTime.Parse(iso8601date,null,DateTimeStyles.RoundtripKind);
// 2014-10-22 works fine: 2014-10-22T00:00
// 2014-10 works fine: 2014-10-01T00:00
// 2014-W43 throws error:
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: The string was not recognized as a valid DateTime. There is an unknown word starting at index 5.
有什么方法可以将这个 2014-W43 解析为我可以在 JSON.net 序列化中使用的时间。也许与 NodaTime?我似乎找不到任何东西。
我可以构建它,但似乎应该有一个解决方案。根据维基百科,它是part of the iso8601 spec。
【问题讨论】:
标签: asp.net url asp.net-web-api iso8601 nodatime