0
I have an api that gets a date (the date is sent as string and received as Datetime):
[HttpPost("ObterDados")]
public JsonResult ObterDados(DateTime data)
{
//codigo
if I post with the parameter in the url the date is received with the month and day reversed (it receives mm/dd/yyyy)
$.post("/api/ObterDados/?data=" + self.Data...
if I send the date inside an object it is received correctly (gets dd/mm/yyyy)
var objeto = {data: "15/12/2017"}
$.post("/api/ObterDados/", objeto....
[HttpPost("ObterDados")]
public JsonResult ObterDados(Filtro filtro)
{
//codigo
Why does this happen and has some way to always receive the date as dd/mm/yyyy ?
Note: The date is always sent as string
Configuration of the Statup class:
var requestLocalizationOptions = new RequestLocalizationOptions
{
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("pt-BR")
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("pt-BR")
},
DefaultRequestCulture = new RequestCulture("pt-BR", "pt-BR"),
};
app.UseRequestLocalization(requestLocalizationOptions);
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
</system.webServer>
</configuration>
Get a look at this question https://stackoverflow.com/questions/24690879/how-use-a-specific-cultureinfo-in-asp-net-web-api
– Zorkind
Just set up Cultureinfo on your Webconfig
– Zorkind
With this configuration Binder will do Datetime.Parse in the default culture of the application, that is, whatever is configured in the config. If you want you can do it via code, but I find it more efficient to do it in Config.
– Zorkind
I put the configuration of the Startup class (Asp.net core 2.0) in the question, give a look, it would be the same thing to do that you suggested in webconfig ?
– Aesir
Gee, it’s different in . net core?
– Zorkind
I think so, since I created the project I haven’t changed the web.config, nor the connection to the bank..
– Aesir
You should have <Globalization> on the web.config I see no reason not to have :-\
– Zorkind
Take a look at the accepted answer there, it puts on the Web.Config, because by Web.Config you talk to the IIS I see no reason for the . net core have modified this.
– Zorkind
I will try the solution of the answer accepted, I put the project web.config in question..
– Aesir
Take a look at the Core version of Globalization https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization
– Zorkind
Send as YYYY-MM-DD, which is an unambiguous format.
– OnoSendai