如何在Asp.Net Core中设置日期绑定的区域性? [英] How to set culture for date binding in Asp.Net Core?

查看:194
本文介绍了如何在Asp.Net Core中设置日期绑定的区域性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有MVC的Asp.Net Core应用程序.我正在提交一个带有日期的表格.

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 EditCustomerViewModel
<form asp-action="Edit">
    <input asp-for="ServiceStartDate" class="form-control" type="date" />
    <input type="submit" value="Update" class="btn btn-success" />
</form>

控制器动作是:

[HttpPost]
public async Task<IActionResult> Edit(EditCustomerViewModel viewModel)
{
    // do stuff
    return RedirectToAction("Index");
}

查看模型为:

public class EditCustomerViewModel
{
    public Guid Id { get; set; }

    [DataType(DataType.Date)]
    public DateTime ServiceStartDate { get; set; }

    [DataType(DataType.Date)]
    public DateTime? ServiceEndDate { get; set; }

    // etc.
}

我在英国,所以日期不是美国格式:dd/MM/YYYY.因此,默认情况下,我正在提交6/22/2017.

I'm in the UK, so dates are not in US format: dd/MM/YYYY. So by default I'm submitting 6/22/2017.

在调试过程中在控制器中查看提交的视图模型时,如果以英国格式提交,则日期为null,但如果使用美国格式,则为日期.即6/22/2017给我null,但是22/6/2017绑定到正确的日期.

When looking on the submitted view model in controller during debugging, dates are null if submitted in UK format, but are fine if using US format. i.e. 6/22/2017 gives me null, but 22/6/2017 is bound to the correct date.

我尝试将其添加到Startup.cs中,但没有任何区别:

I have tried adding this to Startup.cs but it did not make any difference:

var supportedCultures = new[] { new CultureInfo("en-GB") };
app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("en-GB"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures
});
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-GB");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-GB");
CultureInfo.CurrentCulture = new CultureInfo("en-GB");
CultureInfo.CurrentUICulture = new CultureInfo("en-GB");

我已经检查了HTTP标头,并发布了正确的标头:

I've checked HTTP headers and I'm posting correct header:

Accept-Language: en-GB,en

我在做什么错?如何告诉MVC Core活页夹绑定英国格式的日期?

p.s.我在带有* .csproj项目文件,目标框架.NetCoreApp 1.1的VS2017上

p.s. I'm on VS2017 with *.csproj project file, with target framework .NetCoreApp 1.1

推荐答案

几件事.我不确定您是否可以像这样将新的设置对象推送到中间件中(您可能可以),但是大多数情况下,我看到它在ConfigureServices方法中使用的方式如下:

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<RequestLocalizationOptions>(options =>
    {
        options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-NZ");
        options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("en-NZ") };
    });

    services.AddMvc();
}

第二.中间件的顺序非常重要.确保对UseRequestLocalization的调用发生在UseMvc之前.实际上,除非有特殊原因,否则它可能应该是管道中的第一件事.

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();
}

最后,您是否可以尝试从管道中删除所有提供程序(其中一个是cookie提供程序.我无法理解为什么会有这个cookie,但让我们尝试一下).

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).

在您的configure方法中,在RequestCultureProviders列表上清除.这应该确保没有其他地方可以建立一种文化.

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<RequestLocalizationOptions>(options =>
    {
        options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-GB");
        options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-GB") };
        options.RequestCultureProviders.Clear();
    });

    services.AddMvc();
}

更多信息: http://dotnetcoretutorials.com/2017/06/22/request-culture-asp-net-core/

这篇关于如何在Asp.Net Core中设置日期绑定的区域性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆