How to set culture for date binding in Asp.Net Core?

后端 未结 2 480
长情又很酷
长情又很酷 2020-12-15 19:32

I have an Asp.Net Core application with MVC. I\'m submitting a form with a date on the form.

Form looks (roughly) like this:

@model EditCustomerView         


        
2条回答
  •  庸人自扰
    2020-12-15 19:52

    A couple of things. I'm not sure you can push a new settings object into the middleware like that (You probably can), but most of the time I have seen it used in the ConfigureServices method like so :

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure(options =>
        {
            options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-NZ");
            options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("en-NZ") };
        });
    
        services.AddMvc();
    }
    

    Second. The order of your middleware is very important. Ensure that your call to UseRequestLocalization happens before UseMvc. Infact it should probably be the first thing in your pipeline unless there is a specific reason it can't be.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
    
        app.UseRequestLocalization();
        app.UseMvc();
    }
    

    And finally, can you try removing all the providers from the pipeline (One of which is a cookie provider. I can't fathom why you would have this cookie but let's just try).

    In your configure method call clear on the RequestCultureProviders list. This should ensure that there is nothing else there to set a culture.

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure(options =>
        {
            options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-GB");
            options.SupportedCultures = new List { new CultureInfo("en-GB") };
            options.RequestCultureProviders.Clear();
        });
    
        services.AddMvc();
    }
    

    More info : http://dotnetcoretutorials.com/2017/06/22/request-culture-asp-net-core/

提交回复
热议问题