使用asp.net Web API 2属性路由创建可以在URI中接受DateTime的路由 [英] Creating a route that can accept a DateTime in the URI with asp.net web api 2 attribute routing

查看:82
本文介绍了使用asp.net Web API 2属性路由创建可以在URI中接受DateTime的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看是否需要编写自定义的IHttpRouteConstraint,或者是否可以与内置的内容搏斗以获取所需的内容.我看不到任何关于此的好的文档.

I'm trying to see if I need to write a custom IHttpRouteConstraint or if I can wrestle with the built-in ones to get what I want. I can't see to find any good documentation on this anywhere.

基本上,这是我的动作:

Basically, here's my action:

[Route("var/{varId:int:min(1)}/slot/{*slot:datetime}")]
public async Task<HttpResponseMessage> Put(int varId, DateTime slot)
{
    ...
}

我想要的是能够这样称呼它: PUT /api/data/var/1/slot/2012/01/01/131516,并让框架将19绑定到var id和一个DateTime,其值为"slot"值,其值为"Jan 1st,2012,1:15:16 pm".

What I want is to be able to call it like this: PUT /api/data/var/1/slot/2012/01/01/131516 and have the framework bind 19 to var id and a DateTime with a value of "Jan 1st, 2012, 1:15:16pm" as the "slot" value.

从此处遵循该指南: http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing 我可以通过仅传递日期段(即PUT /api/data/var/1/slot/2012/01/01PUT /api/data/var/1/slot/2012-01-01)来使其工作,但这只给了我一个数据值,没有时间成分.

Following the guide from here: http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing I am able to get it to work by passing in just the date segments, i.e. PUT /api/data/var/1/slot/2012/01/01 or PUT /api/data/var/1/slot/2012-01-01, but that only gives me a data value, no time components.

某事告诉我,尝试以任何合理的方式在URI段中进行时间传递是一个坏主意,但是我不确定为什么这会是个坏主意,除了在本地时间与UTC时间之间存在歧义.

Something tells me that trying to pass in time in any sane way through URI segments is a bad idea, but I'm not sure why it'd be a bad idea, besides the ambiguity regarding local vs UTC times.

我还尝试使用正则表达式来限制datetime约束,例如{slot:datetime:regex(\\d{4}/\\d{2}/\\d{2})/\\d{4})}尝试使其解析为2013/01/01/151617之类的日期时间,但无济于事.

I've also tried constraining the datetime constraint with a regex, e.g. {slot:datetime:regex(\\d{4}/\\d{2}/\\d{2})/\\d{4})} to try to get it to parse something like 2013/01/01/151617 as a DateTime, but to no avail.

我很确定我可以将其与自定义IHttpRouteConstraint一起使用,我只是不想做可能内置的事情.

I'm pretty sure I can get this to work with a custom IHttpRouteConstraint, I just don't want to do something that might be built in.

谢谢!

推荐答案

Web API日期时间约束对于解析日期时间没有任何特殊作用,您可以在下面注意到(源代码

Web API datetime constraint doesn't do anything special regarding parsing datetime as you can notice below(source code here). If your request url is like var/1/slot/2012-01-01 1:45:30 PM or var/1/slot/2012/01/01 1:45:30 PM, it seems to work fine...but I guess if you need full flexibility then creating a custom constraint is the best option...

public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
    if (parameterName == null)
    {
        throw Error.ArgumentNull("parameterName");
    }

    if (values == null)
    {
        throw Error.ArgumentNull("values");
    }

    object value;
    if (values.TryGetValue(parameterName, out value) && value != null)
    {
        if (value is DateTime)
        {
            return true;
        }

        DateTime result;
        string valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
        return DateTime.TryParse(valueString, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
    }
    return false;
}

这篇关于使用asp.net Web API 2属性路由创建可以在URI中接受DateTime的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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