当前位置 > 首页 > Asp.net

Web Site Globalization With ASP.NET Routing

2010-2-1 12:02:00来源:Asp.net

For those who don’t know, I have this web site http://PauloMorgado.NET/ that I use both as a web presence besides my blogs and a playfield.

Because I write both in English and Portuguese, I wanted the web site to have both English and Portuguese versions. This is easily accomplished by using ASP.NET Globalization and Localization.

But I wanted to do more than guessing the user’s language form her/his web browser languages. I wanted something like the MSDN and TechNet web sites have where the culture is embedded in the URL which makes it easy for the user to choose in which language she/he wants to see the web site.

With the release of the ASP.NET Routing, this is as easy as writing a custom route handler that sets the culture for the request and returns the requested page handler.

Something like this:

public class GlobalizationRouteHandler : global::System.Web.Routing.IRouteHandler
{
    System.Globalization.CultureInfo culture;
    System.Globalization.CultureInfo uiCulture;

    public GlobalizationRouteHandler(System.Globalization.CultureInfo culture)
        : this(culture, culture)
    {
    }

    public GlobalizationRouteHandler(CultureInfo culture, CultureInfo uiCulture)
    {
        if (culture == null)
        {
            throw new ArgumentNullException("cultureInfo", "cultureInfo is null.");
        }

        if (uiCulture == null)
        {
            throw new ArgumentNullException("uiCulture", "uiCulture is null.");
        }

        this.culture = culture;
        this.uiCulture = uiCulture;
    }

    private GlobalizationRouteHandler()
    {
    }

    #region IRouteHandler Members

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        Thread.CurrentThread.CurrentCulture = this.culture;
        Thread.CurrentThread.CurrentUICulture = this.uiCulture;

        string path = "~/" + (requestContext.RouteData.Values["path"] as string);

        var physicalPath = requestContext.HttpContext.Server.MapPath(path);
        if (System.IO.Directory.Exists(physicalPath))
        {
            path = VirtualPathUtility.Combine(path, "Default.aspx");
        }

        var httpHandler = BuildManager.CreateInstanceFromVirtualPath(path, typeof(IHttpHandler)) as IHttpHandler;

        return httpHandler;
    }

    #endregion
}

And now it’s only a matter of registering the handled cultures:

routes.Add("en", new Route("en/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("en-US"))));
routes.Add("pt", new Route("pt/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("pt-PT"))));


  • WF的一些技术细节与应用体会(三)

    2007-2-16 10:00:00

    摘要:   有关HandleExternalEvent的一些事情。有一个很有趣的现象,是望星观月发现的。情况大体是这样的,当一个工作流设计中含有一个HandleExternalEvent活动时,在其

  • Create a Login Screen in Silverlight 3

    2009-10-30 6:52:00

    After my last blog post on “Create a Login Window in WPF”, I had a lot of requests for how to creat

  • 用asp.net开发移动wap网站集成在线wap模拟器

    2009-10-12 13:52:00

    摘要: 俺用的工具有点落后,vs2008 express版。没用过asp.net mobile 开发过wap网站,于是想尽各种办法用asp.net 给实现了:刚出了点状况,代码下载下载源代码注:通过

  • 老南瓜:C#通过AMO对象模型浏览SQL SERVER 2005 SSAS角色一例

    2009-8-31 9:22:00

    摘要: C#通过AMO对象模型浏览SQL SERVER 2005 SSAS角色一例用过SSAS的朋友都使用Visual Studio Business Intelligence Developmen

  • 阻止冒泡事件

    2009-10-17 6:04:00

    摘要: 阻止冒泡事件的方法有两种,第一种是IE的方法,第二种是DOM方法 作者资料: 铁拐李 我的主页 个人资料 我的闪存 与我联系 ------------------

  • 【ASP.NET】困扰我多年的URL参数设置问题

    2011-3-9 13:02:00

    URL参数一直以来困扰着我。始终没有一个完美的解决方案!考虑到网页的安全性有些地方不得不做以限制。由于本身对这个地方没有经验,所以前来问问各路高手 你们是怎么处理这个问题的。我把我所遇到的问题说一下

  • Code Signing as Reverse Engineering Protection

    2007-9-16 5:48:00

    In my last post I spoke about reverse engineering .NET assemblies upon which Gregory asked ho

  • 这两天自己模仿写的一个Asp.Net的显示分页方法 附加实体转换和存储过程

    2012-3-7 20:51:00

    之前自己一直用Aspnetpager控件来显示项目中的分页,但是每次都要拖一个aspnetpager的控件进去,感觉很不舒服,因为现在自己写的webform都不用服务器控件了,所以自己仿照aspne

  • 深入探讨ASP.NET MVC的筛选器

    2012-7-2 8:49:00

    在ActionInvoker对Action的执行过程中,除了通过利用ActionDescriptor对Action方法的执行,以及之前进行的Model绑定与验证之外,还具有一个重要的工作,那就是对相

  • iOS 4 vs Exchange

    2010-7-19 8:02:00

    If you’re experiencing problems with your iOS 4 device and your Exchange server, be advised that th