如何设置全球化? [英] How to set globalization?
问题描述
我陷入了ASP.NET Web API全球化问题.我想在调用Web API时以特定于文化的格式获取日期时间.请提供一些见解.
I'm stuck in an ASP.NET Web API Globalization problem. I want to get date time in culture specific format when I invoke a Web API. Please provide some insights.
在我的解决方案中,首先,有一个委派处理程序,其任务是设置区域性.定义如下:
In my solution, first, there is one delegating handler, whose job is to set the culture. It is defined as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace CultureHandling.Handlers
{
public class CultureHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request != null && request.Headers != null && request.Headers.Count() > 0)
{
var reqHdrs = request.Headers.AcceptLanguage;
var headerValue = reqHdrs.OrderByDescending(e => e.Quality ?? 1.0D)
.Where(e => !e.Quality.HasValue || e.Quality.Value > 0.0D)
.First();
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo(headerValue.Value.ToString());
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.GetCultureInfo(headerValue.Value.ToString());
}
return base.SendAsync(request, cancellationToken);
}
}
}
}
然后,我将日期时间转换器定义为:
Then, I have a date time converter defined as:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace CultureHandling.Converters
{
public class CustomDateTimeConverter : DateTimeConverterBase
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return (DateTime.Parse(reader.Value.ToString()));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((DateTime)value).ToString());
}
}
}
非常简单的模型类是:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Doj { get; set; }
}
相应的控制器动作是:
public class EmployeeController : ApiController
{
private readonly List<Employee> employees = null;
public EmployeeController()
{
employees = new List<Employee>();
employees.Add(new Employee { Id = 1, Name = "Employee1", Doj = new DateTime(2014, 7, 1)});
employees.Add(new Employee { Id = 2, Name = "Employee2", Doj = new DateTime(2015, 7, 1)});
}
public List<Employee> Get()
{
return employees;
}
}
我已将WebApiConfig.cs配置为:
I've configured the WebApiConfig.cs as:
config.MapHttpAttributeRoutes();
config.MessageHandlers.Add(new CultureHandler());
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new CustomDateTimeConverter());
现在,问题所在: 理想情况下,如果我使用 en-US 接受语言来调用API,则它应返回日期时间值为 MM/dd/yyyy 格式,并且返回 fr-FR ,格式为 dd/MM/yyyy .但是,对于这两种情况,我都获得了MM/dd/yyyy格式的价值.
Now, the problem part: Ideally if I invoke the API with Accept Language as en-US, it should return date time value is MM/dd/yyyy format and for fr-FR, the format is dd/MM/yyyy. But, for both of these cases, I'm getting value in MM/dd/yyyy format.
根据调查,我发现CultureHandler运行正常,这是邮递员的屏幕截图:
As per investigation, I found the CultureHandler to be working as expected, here is a screenshot of Postman:
CultureHandler.cs中的断点:
The break-point in CultureHandler.cs:
但是,令人惊讶的是,在模型绑定期间,我看到了将文化更改为en-US:
But, surprisingly, during model binding, I'm seeing the culture to be changed to en-US:
我相信这就是为什么当文化是fr-FR时,我无法获得 dd/MM/yyyy 格式的日期时间值的原因:
I believe this is the reason why I'm not getting date time value in dd/MM/yyyy format when culture is fr-FR:
所以,我的问题是为什么这种文化在哪里发生变化?我在编码中犯任何错误吗?预先感谢!
So, my question is why and where does this culture getting changed? Am I making any mistake in coding? Thanks in advance!
推荐答案
我创建了一个新的空项目,然后在解决方案中编写了所有共享代码.您的代码是正确的.屏幕截图如下
I create a new empty project then i wrote all shared code in my solution. Your code is work true. Screenshots is below
我尝试过邮递员.
这篇关于如何设置全球化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!