ModelState中确认文化对HttpPost [英] ModelState Validation Culture on HttpPost
问题描述
有谁知道,如果你能控制的ModelState对象的文化,面对我的多语言应用程序的一个问题IM,其中语言是基于从访问站点前其子:
Does anyone know if you can control the culture of the ModelState Object, im facing a problem in my multilingual application where the language is based on which subdomain from which you visit the site ex:
italia.domain.com - 改变文化意大利
italia.domain.com - "Changes the culture to Italian"
german.domain.com - 改变文化的德国
german.domain.com - "Changes the culture to German"
问题是,ModelState中对象的语言时,提交表单似乎是由客户端浏览器进行控制,而不是当前线程的文化。
The issue is that the language on the ModelState object when submitting a form seems to be controlled by the Clients browser and not the current threads culture.
因此,进出口寻找一个解决方案,我可以修改此行为或覆盖它,使我的意大利子域的语言始终是意大利人,而不是客户端浏览器的语言。
So im searching for a solution where i can modify this behavior or override it so that the language on my italian subdomain always is italian and not the language of the clients browser.
修改
我已经作出在那里我改变基于子语言的一部分:
var HttpHost = HttpContext.Request.ServerVariables["HTTP_HOST"];
var _hostname = (HttpHost.Split(':').Length > 1) ? HttpHost.Substring(0, HttpHost.IndexOf(':')) : HttpHost;
var allowedHostnames = "italiansubdomain.domain.com|it,frenchsubdomain.domain.com|fr,germansubdomain.domain.com|de,englishsubdomain.domain.com|en".Split(',');
foreach (var hostname in allowedHostnames)
{
if (hostname.StartsWith(_hostname.ToLower()))
{
var lang = hostname.Split('|').Last();
if (lang == "en") lang = "uk";
// Updates the cultures for the dynamic language
var ci = new CultureInfo(lang);
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
}
}
所以我的问题是,当我使用的ModelState验证这样的:
So my problem is that when i use Modelstate validation like this:
public class Email {
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), StringLength(50, MinimumLength = 2, ErrorMessageResourceName = "validation_string_length_2_50", ErrorMessageResourceType = typeof(Resources.Master))]
public string SenderName { get; set; }
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), Email(ErrorMessageResourceName = "validation_email_invalid", ErrorMessageResourceType = typeof(Resources.Master))]
public string SenderEmail { get; set; }
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), StringLength(50, MinimumLength = 2, ErrorMessageResourceName = "validation_string_length_2_50", ErrorMessageResourceType = typeof(Resources.Master))]
public string ReceiverName { get; set; }
[Required(ErrorMessageResourceName = "validation_required", ErrorMessageResourceType = typeof(Resources.Master)), Email(ErrorMessageResourceName = "validation_email_invalid", ErrorMessageResourceType = typeof(Resources.Master))]
public string ReceiverEmail { get; set; }
public string Comment { get; set; }
}
和在检查的ModelState验证部分:
And the check for modelstate validation part:
if (!ModelState.IsValid) {
var keys = ModelState.Keys.ToList();
var values = ModelState.Values.ToList();
for (var i = 0; i < keys.Count; i++)
{
var value = values[i];
if (value.Errors.Count > 0)
{
response.AddError(keys[i], value.Errors[0].ErrorMessage);
}
}
}
当我再通过Ajax响应结果,或只是调试IM基于浏览器的语言设置收到错误信息访问的错误,这就是我要改变它是当前处于活动状态,而不是语言。
When i then access the Errors through a Ajax Response result or just by debugging im receiving the error messages based on the browsers language setting, this is where i want to change it to be the language that is currently active instead.
修改
由于提前,bsthomsen
Thanks in advance, bsthomsen
推荐答案
我解决了这个通过移动code那里的文化是从Controller.OnActionExecuting到Controller.ExecuteCore改变
I solved this by moving the code where the culture is being changed from Controller.OnActionExecuting to Controller.ExecuteCore
这篇关于ModelState中确认文化对HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!