好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在ASP.NET MVC中通过URL路由实现对多语言的支持

在ASP.NET MVC中通过URL路由实现对多语言的支持

对于一个需要支持多语言的Web应用,一个很常见的使用方式就是通过请求地址来控制界面呈现所基于的语言文化,比如我们在表示请求地址的URL中将上语言文化代码(比如en或者en-US)来指导服务器应该采用怎样的语言来显示界面的内容。对于一个ASP.NET MVC应用来说,我们很容易通过URL路由来实现这样一个功能。[本文已经同步到《 How ASP.NET MVC Works? 》中]

在具体介绍实现之前,我们通过一个简单的例子谈谈最终实现的效果。在通过ASP.NET MVC项目模板创建的空Web应用中,我们创建了如下一个HomeController,默认的Action方法Index用于呈现一个登录View。作为Model的LoginInfo类包含UserName和Password两个属性,分别表示登录输入的用户名和密码。需要注意的是,在两个属性上应用了DisplayAttribute并通过资源的方式指定了显示名称以实现对多语言的支持。[原代码从 这里 下载]

    1:   public   class  HomeController : Controller
    2:  {
    3:       public  ActionResult Index()
    4:      {
    5:           return  View( new  LoginInfo());
    6:      }
    7:  }
    8:   
    9:   public   class  LoginInfo
   10:  {
   11:      [Display(Name = "UserName" , ResourceType =  typeof (Resources))]
   12:       public   string  UserName { get; set; }
   13:   
   14:      [Display(Name= "Password" , ResourceType =  typeof (Resources))]
   15:      [DataType(DataType.Password)]
   16:       public   string  Password { get; set; }
   17:  }

如下所示的Action方法Index对应的View的定义,这是一个基于LogInfo的强类型View。

    1:  @model MvcApp.Models.LoginInfo
    2:  @ using  (Html.BeginForm())
    3:  { 
    4:      @Html.EditorForModel()
    5:      <input type= "submit"   value = "@MvcApp.Properties.Resources.Login"  />
    6:      <input type= "button"   value = "@MvcApp.Properties.Resources.Cancel"  />
    7:  }

在Global.asax中,我们修改了默认添加的URL路由注册代码,使请求URL中包含相应的语言文化信息({culture})。

    1:   public   class  MvcApplication : System.Web.HttpApplication
    2:  {
    3:       //其他成员 
    4:       public   static   void  RegisterRoutes(RouteCollection routes)
    5:      {
    6:            //其他操作 
    7:          routes.MapRoute(
    8:              name:  "Default" ,
    9:              url:  "{culture}/{controller}/{action}/{id}" ,
   10:              defaults:  new  { culture= "en" , controller =  "Home" , action =  "Index" , id = UrlParameter.Optional }
   11:          );
   12:      }
   13:  }

我们直接运行该程序,并在请求地址中指定不同的Culture(en和zh),界面呈现基于的语言正是我们期望的。

实际上针对URL路由的本地化可以通过具有如下定义的名为CultureAwareHttpModule的自定义HttpModule来实现。我们通过CultureAwareHttpModule注册了HttpApplication的BeginRequest和EndRequest事件,通过URL路由系统得到表示语言文化的路由变量culture,并对当前线程的Culture和UICulture进行了相应的设置和恢复。

    1:   public   class  CultureAwareHttpModule : IHttpModule
    2:  {
    3:       private  CultureInfo currentCulture;
    4:       private  CultureInfo currentUICulture;
    5:   
    6:       public   void  Dispose(){}
    7:       public   void  Init(HttpApplication context)
    8:      {
    9:          context.BeginRequest += SetCurrentCulture;
   10:          context.EndRequest   += RecoverCulture;
   11:      }
   12:       private    void  SetCurrentCulture( object  sender, EventArgs args)
   13:      {
   14:          currentCulture   = Thread.CurrentThread.CurrentCulture;
   15:          currentUICulture = Thread.CurrentThread.CurrentUICulture;
   16:          HttpContextBase contextWrapper =  new  HttpContextWrapper(HttpContext.Current);
   17:          RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper);
   18:           object  culture;
   19:           if  (routeData.Values.TryGetValue( "culture" ,  out  culture))
   20:          {
   21:   
   22:               try 
   23:              {
   24:                  Thread.CurrentThread.CurrentCulture   =  new  CultureInfo(culture.ToString());
   25:                  Thread.CurrentThread.CurrentUICulture =  new  CultureInfo(culture.ToString());
   26:              }
   27:               catch 
   28:              { }
   29:          }
   30:      }
   31:       private   void  RecoverCulture( object  sender, EventArgs args)
   32:      {
   33:          Thread.CurrentThread.CurrentCulture   = currentCulture;
   34:          Thread.CurrentThread.CurrentUICulture = currentUICulture;
   35:      }
   36:  }

我们只需要通过如下配置对CultureAwareHttpModule进行注册即可。

    1:   <  configuration  > 
    2:     <  system.web  > 
    3:       <  httpModules  > 
    4:         <  add   name  ="CultureAwareHttpModule"   type  ="MvcApp.CultureAwareHttpModule, MvcApp"  /> 
    5:       </  httpModules  > 
    6:      ...
    7:   </  configuration  > 

作者: Artech
出处: http://artech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

分类:  [02] 编程技巧

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于在ASP.NET MVC中通过URL路由实现对多语言的支持的详细内容...

  阅读:44次