Search code examples
asp.net-web-apiasp.net-web-api-routing

Creating a route that can accept a DateTime in the URI with asp.net web api 2 attribute routing


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)
{
    ...
}

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.

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.

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.

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.

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.

Thanks!


Solution

  • 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;
    }