Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a datetime parameter?

How to pass UTC dates to Web API?

Passing 2010-01-01 works fine, but when I pass a UTC date such as 2014-12-31T22:00:00.000Z (with a time component), I get a HTTP 404 response. So

http://domain/api/controller/action/2012-12-31T22:00:00.000Z 

yields a 404 error response, while

http://domain/api/controller/action/2012-12-31 

works fine.

How to pass UTC dates to Web API then - or at least specify date and time?

like image 632
Nickolodeon Avatar asked Jan 16 '13 13:01

Nickolodeon


People also ask

How do I pass a DateTime parameter to a SQL stored procedure?

CREATE PROCEDURE GetData ( @ID INT = NULL , @StartDate DATETIME = NULL , @EndDate DATETIME = NULL ) AS SET NOCOUNT ON; SELECT * FROM TableA A JOIN TableB B ON A. a = B. b WHERE ( @ID IS NULL OR S.ID = @ID ) AND ( @StartDate IS NULL AND @EndDate IS NULL OR DateColumn BETWEEN @StartDate AND @EndDate );

How do I pass DateTime in query string?

string dateTime = Request. QueryString["DateStarted"]; DateTime dt = Convert. ToDateTime(dateTime); Response.

How do you pass DateTime parameters in Postman?

In the Params tab, enter From in the Key column. Add the start date of your timeframe in the Value column — this must be in ISO format ( YYYY-MM-DD ). In the row below, enter To in the Key column and the end date of your timeframe in the Value column.

How do you pass a date as a parameter in Java?

Using Date ClassThe usage of format() method of the DateFormat class is made to pass the date object as a parameter to the method. DateFormat dform = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ); Date obj = new Date(); System.


2 Answers

The problem is twofold:

1. The . in the route

By default, IIS treats all URI's with a dot in them as static resource, tries to return it and skip further processing (by Web API) altogether. This is configured in your Web.config in the section system.webServer.handlers: the default handler handles path="*.". You won't find much documentation regarding the strange syntax in this path attribute (regex would have made more sense), but what this apparently means is "anything that doesn't contain a dot" (and any character from point 2 below). Hence the 'Extensionless' in the name ExtensionlessUrlHandler-Integrated-4.0.

Multiple solutions are possible, in my opinion in the order of 'correctness':

  • Add a new handler specifically for the routes that must allow a dot. Be sure to add it before the default. To do this, make sure you remove the default handler first, and add it back after yours.
  • Change the path="*." attribute to path="*". It will then catch everything. Note that from then on, your web api will no longer interpret incoming calls with dots as static resources! If you are hosting static resources on your web api, this is therefor not advised!
  • Add the following to your Web.config to unconditionally handle all requests: under <system.webserver>: <modules runAllManagedModulesForAllRequests="true">

2. The : in the route

After you've changed the above, by default, you'd get the following error:

A potentially dangerous Request.Path value was detected from the client (:).

You can change the predefined disallowed/invalid characters in your Web.config. Under <system.web>, add the following: <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,%,&amp;,*,\,?" />. I've removed the : from the standard list of invalid characters.

Easier/safer solutions

Although not an answer to your question, a safer and easier solution would be to change the request so that all this is not required. This can be done in two ways:

  1. Pass the date as a query string parameter, like ?date=2012-12-31T22:00:00.000Z.
  2. Strip the .000 from every request. You'd still need to allow :'s (cfr point 2).
like image 126
Vincent Sels Avatar answered Sep 26 '22 22:09

Vincent Sels


in your Product Web API controller:

[RoutePrefix("api/product")] public class ProductController : ApiController {     private readonly IProductRepository _repository;     public ProductController(IProductRepository repository)     {         this._repository = repository;     }      [HttpGet, Route("orders")]     public async Task<IHttpActionResult> GetProductPeriodOrders(string productCode, DateTime dateStart, DateTime dateEnd)     {         try         {             IList<Order> orders = await _repository.GetPeriodOrdersAsync(productCode, dateStart.ToUniversalTime(), dateEnd.ToUniversalTime());             return Ok(orders);         }         catch(Exception ex)         {             return NotFound();         }     } } 

test GetProductPeriodOrders method in Fiddler - Composer:

http://localhost:46017/api/product/orders?productCode=100&dateStart=2016-12-01T00:00:00&dateEnd=2016-12-31T23:59:59 

DateTime format:

yyyy-MM-ddTHH:mm:ss 

javascript pass parameter use moment.js

const dateStart = moment(startDate).format('YYYY-MM-DDTHH:mm:ss'); const dateEnd = moment(endDate).format('YYYY-MM-DDTHH:mm:ss'); 
like image 25
FreeClimb Avatar answered Sep 22 '22 22:09

FreeClimb