asp.net – 如何控制显示模型验证错误的语言

在ASP.NET MVC应用程序中,更改Thread.CurrentThread.CurrentCulture [UI]可以改变MVC从资源中选择消息的方式.在我创建的示例应用程序中,有两个资源文件 – 英语消息的Res.resx和西班牙语消息的Res.es.resx.

但是,模型验证产生的错误消息始终以英语显示.

我的问题是,如何控制模型验证错误消息的显示语言?

下面是我编写的示例应用程序的一部分(基于默认的ASP.NET MVC应用程序)来演示此问题.

它在浏览器中的外观截图:

https://dl.dropboxusercontent.com/u/4453002/SO_LanguageOfValidation.png

ViewModel和Controller – HomeController.cs:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;

namespace SO_ValidationMessageInEnglish.Controllers {

  /// <summary>
  /// A very basic view model.
  /// </summary>
  public class ViewModel {

    [Display(Name = "Message", ResourceType = typeof(Res))]
    [DisplayName("Message")]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "MessageRequired", ErrorMessageResourceType = typeof(Res))]
    public string Message { get; set; }

    public string Language { get; set; }
  }

  public class HomeController : Controller {

    public ActionResult Index(string language = "en") {
      Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
      return View();
    }

    [HttpPost]
    [ActionName("Index")]
    public ActionResult IndexPost(ViewModel foo) {
      Thread.CurrentThread.CurrentCulture = new CultureInfo(foo.Language ?? "en");
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(foo.Language ?? "en");
      return View(foo);
    }

  }
}

查看 – Index.cshtml:

@model SO_ValidationMessageInEnglish.Controllers.ViewModel
@using SO_ValidationMessageInEnglish
@{ ViewBag.Title = Res.Title; }
@Res.CurrentMessage:<br />
<h2>@((Model != null) ? Model.Message : Res.Default)</h2>
<p />    
@using (Html.BeginForm("Index", "Home", FormMethod.Post, null)) {        
    @Html.LabelFor(m => m.Message)    
    @Html.TextBoxFor(m => m.Message)
    @Html.HiddenFor(m => m.Language)
    @Html.ValidationMessageFor(m => m.Message)
    <input type="submit" value="@Res.Submit" />
}

最佳答案 我也遇到了同样的问题.当模型绑定器具有无效数据时,它将在ActionFilter之前运行.

我不喜欢提出的解决方案,因为弄乱路由并不是我的首选解决方案.侦听Application_AcquireRequestState是有问题的,因为此事件会触发每个请求,而不仅仅是针对将被路由到MVC控制器的请求.

我最终编写了一个IControllerFactory的自定义实现,它在内部使用DefaultControllerFactory并在CreateController方法中执行本地化代码.
这也不理想,希望它有所帮助.

  public class PluggableControllerFactory : IControllerFactory {

    private readonly IControllerFactory innerControllerFactory;

    public PluggableControllerFactory() {
      innerControllerFactory = new DefaultControllerFactory();
    }

    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) {
      // Run your culture localization here

      return innerControllerFactory.CreateController(requestContext, controllerName);
    }

    public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName) {
      return innerControllerFactory.GetControllerSessionBehavior(requestContext, controllerName);
    }

    public void ReleaseController(IController controller) {
      innerControllerFactory.ReleaseController(controller);
    }

  }
}
点赞