问题
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());
}
}
}
The very simple model class is:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Doj { get; set; }
}
And the corresponding controller action is:
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;
}
}
I've configured the WebApiConfig.cs as:
config.MapHttpAttributeRoutes();
config.MessageHandlers.Add(new CultureHandler());
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new CustomDateTimeConverter());
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.
As per investigation, I found the CultureHandler to be working as expected, here is a screenshot of Postman:
The break-point in CultureHandler.cs:
But, surprisingly, during model binding, I'm seeing the culture to be changed to en-US:
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!
回答1:
I create a new empty project then i wrote all shared code in my solution. Your code is work true. Screenshots is below
I tried with postman.
来源:https://stackoverflow.com/questions/60648268/how-to-set-globalization