Passing Datetimeoffset as Webapi Query String

Passing DateTimeOffset as WebAPI query string

The problem is being described exactly by the 400 response message, although it could have been more clear. The route, as defined by the attribute, only expects a parameter id, but the Delete method expects another parameter called date.

If you want to provide this value using the query string, you'll need to make that parameter nullable, by using "DateTimeOffset?", which would also transform it into an optional parameter. If the date is a required field, consider adding it to the route, like:

[Route("api/values/{id}/{date}")]

OK, ignore what I typed above, it's just a formatting problem. Web API has trouble figuring out the culture needed to parse the given value, but if you try to pass on DateTimeOffset using a JSON format in the query string, like 2014-05-06T22:24:55Z, that should work.

Passing DateTimeOffset as WebAPI query string

The problem is being described exactly by the 400 response message, although it could have been more clear. The route, as defined by the attribute, only expects a parameter id, but the Delete method expects another parameter called date.

If you want to provide this value using the query string, you'll need to make that parameter nullable, by using "DateTimeOffset?", which would also transform it into an optional parameter. If the date is a required field, consider adding it to the route, like:

[Route("api/values/{id}/{date}")]

OK, ignore what I typed above, it's just a formatting problem. Web API has trouble figuring out the culture needed to parse the given value, but if you try to pass on DateTimeOffset using a JSON format in the query string, like 2014-05-06T22:24:55Z, that should work.

Passing DateTimeOffset as Route Attribute

You're injecting a DateTimeOffset into DatePeriodRepository, however your DI setup is configuring an Options class.

Change DatePeriodRepository to expect the configured Options class:

internal class DatePeriodRepository: IDatePeriodRepository
{
private readonly DateTimeOffset _dateCycleStart;

public DatePeriodRepository(Options options)
{
_dateCycleStart = options.BillingPeriodCycleStart;
}

public Task<int> GetDatePeriod()
{
return GetDatePeriod(DateTimeOffset.Now);
}

public Task<int> GetDatePeriod(DateTimeOffset date)
{
var yearDiff = (date.Year - _billingCycleStart.Year) * 12;
var monthDiff = yearDiff + date.Month - _dateCycleStart.Month;
return Task.FromResult(monthDiff);
}
}

C# Web Api Pass Datetime Parameter

I believe the problem is related to the date format you are using to pass those values. The space between the date part and the hour part is replaced by %20 and the resulting text can't be parsed as a datetime.

You could try using this format: YYYY-MM-DDTHH:mm:SS

Like this:

http://localhost:7150/api/logLoginDetails/GetLogLoginDetails?userId=&userIp=192.168.2.1&startTime=2020-09-22T18:07:13&endTime=2020-09-23T18:07:13

Or, if you really need to use that format, maybe you could change the types to strings. Like this:

public HttpResponseMessage GetLogLoginDetails(int? userId = null, string userIp = null, string startTime = null, string endTime = null)
{
try
{
return CROk(logLoginDetailsService.GetLogLoginDetails(userId: userId, userIp: userIp, startTime: startTime, endTime: endTime));
}
catch (Exception e)
{
return CRException(e);
}
}

ASP.NET DateTime Serialization in QueryString vs Body

Unfortunately that's the way it is, have a look at this answer - Passing UTC DateTime to Web API HttpGet Method results in local time

It's related to how query string parameters and message bodies are treated differently, Model binding vs parameter binding.

You will have to either call .ToUniversalTime() or implement your own model binder to get around this.



Related Topics



Leave a reply



Submit