title: Creating a route that can accept a DateTime in the URI with asp.net web api 2 attribute routing
body: <p>I'm trying to see if I need to write a custom <code>IHttpRouteConstraint</code> 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. </p>
  
  <p>Basically, here's my action:</p>
  
  <pre><code>[Route("var/{varId:int:min(1)}/slot/{*slot:datetime}")]
  public async Task&lt;HttpResponseMessage&gt; Put(int varId, DateTime slot)
  {
      ...
  }
  </code></pre>
  
  <p>What I want is to be able to call it like this:
  <code>PUT /api/data/var/1/slot/2012/01/01/131516</code> and have the framework bind 19 to var id and a <code>DateTime</code> with a value of "Jan 1st, 2012, 1:15:16pm" as the "slot" value. </p>
  
  <p>Following the guide from here: <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing" rel="nofollow">http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing</a> I am able to get it to work by passing in just the date segments, i.e. <code>PUT /api/data/var/1/slot/2012/01/01</code> or <code>PUT /api/data/var/1/slot/2012-01-01</code>, but that only gives me a data value, no time components. </p>
  
  <p>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. </p>
  
  <p>I've also tried constraining the <code>datetime</code> constraint with a regex, e.g. <code>{slot:datetime:regex(\\d{4}/\\d{2}/\\d{2})/\\d{4})}</code> to try to get it to parse something like <code>2013/01/01/151617</code> as a DateTime, but to no avail. </p>
  
  <p>I'm pretty sure I can get this to work with a custom <code>IHttpRouteConstraint</code>, I just don't want to do something that might be built in. </p>
  
  <p>Thanks!</p>