0

I have .net core 2.1 Web Api project. And I'm sending GET request like below. I'm sending "10.12.2019" by postman. But I'm getting from controller as "12.10.2019". There was this issue in POST request. I fixed by adding below code to Startup.cs.But, problem continueing in GET request. How can I fix for GET request?

MyController.cs

[HttpGet]
public void MyGetMethod(DateTime myDate)
{ }

My Url

http://localhost:5012/api/MyController/MyGetMethod?myDate=10.12.2019

My Startup.cs

   services.AddMvc().AddJsonOptions(options =>
   {
       options.SerializerSettings.Culture = new CultureInfo("tr-TR");
   });

///////////////////////////////
   var defaultCulture = new CultureInfo("tr-TR");
   app.UseRequestLocalization(new RequestLocalizationOptions
   {
       DefaultRequestCulture = new RequestCulture(defaultCulture),
       SupportedCultures = new List<CultureInfo> { defaultCulture },
       SupportedUICultures = new List<CultureInfo> { defaultCulture }
   });
2
  • Not that it is a solution to this particular issue per se, but I would accept a string as input and then make a DateTime.TryParse with that string input from the GET-request. More like a work-around, but would be my advice. This would allow you to target the exact formatting you'd like. Commented Jan 18, 2019 at 10:43
  • I don't want taking as string. Because, in that case, I must do datetime parsing in everywehere. So, I want to fix my localization. But, I didn't understand the reason of this bug. @jwweiler Commented Jan 18, 2019 at 10:46

1 Answer 1

0

ok I can see one solution for this problem. You pass it into the body (as data) and pass the format with it. For example

new Date().toUTCString()

and on the server you can receive it like

public void MyGetMethod([FromBody] DateTime myDate)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes. It maybe solve this issue @Gaurav. But, I don't want changing my parameters. I want to solve issue from C# side.
Then you need to use custom model binder for date time. Using this link for details vickram.me/custom-datetime-model-binding-in-asp-net-web-api
That link for asp.net. But, I'm using asp.net Core. So, I couldn't use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.