如何在 Asp.Net Core 中为日期绑定设置文化? [英] How to set culture for date binding in Asp.Net Core?

查看:20
本文介绍了如何在 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.

在调试期间在控制器中查看提交的视图模型时,如果以英国格式提交,则日期为空,但如果使用美国格式则没问题.即 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 绑定器以英国格式绑定日期?

附言我在 VS2017 上使用 *.csproj 项目文件,目标框架为 .NetCoreApp 1.1

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

在您的配置方法调用中清除 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天全站免登陆