【问题标题】:How to Make Language Specific Url in Asp.Net Project?如何在 Asp.Net 项目中制作语言特定的 URL?
【发布时间】:2015-07-01 06:35:04
【问题描述】:

我在我的项目中使用了西风全球化库http://west-wind.com/westwind.globalization/ 来实现多种语言。一切正常。 只有一个问题是我在 url 的末尾得到了文化参考变量http://localhost:18106/LangTest?LocaleId=en

我想要域和页面名称之间的文化引用变量,例如 http://localhost:18106/en/LangTest

实际上,我想像微软网站http://www.microsoft.com/en-us/default.aspx 那样做。

【问题讨论】:

    标签: c# asp.net localization url-routing westwind-globalization


    【解决方案1】:

    您需要使用 IRouteHandler 并且需要创建自定义路由。

    默认情况下,Web 表单使用文件系统请求处理。例如,MyWebsite/contact.aspx 请求搜索位于根菜单下的 contact.aspx 文件。 MyWebsite/superheroes/superheroes.aspx 应该在根菜单下的 superheroes 文件中查找 superheroes.aspx 文件。

    使用 global.asax 文件,您可以添加我们自己的处理。 您只需要从 Application_Start 方法中调用具有 RouteCollection 参数的方法。 因此,您需要创建一个名为 RegisterRoutes 的方法并将其放在一个名为 MyRouteConfig.cs

    的新文件中

    示例如下:

    using System;
    using Microsoft.AspNet.FriendlyUrls;
    using System.Web.Routing;
    using System.Web;
    using System.Web.UI;
    using System.Web.Compilation;
    
    
    public static class MyRouteConfig
    {
       public static void RegisterRoutes(RouteCollection routes)
       {
            routes.EnableFriendlyUrls();     
            routes.MapPageRoute("superheroes", "superhero/{SuperheroName}", "~/superheroes.aspx");
            routes.MapPageRoute("languageSuperheroes", "{language}/superhero/{SuperheroName}", "~/superheroes.aspx");     
            routes.Add(new System.Web.Routing.Route("{language}/{*page}", new LanguageRouteHandler()));
      } 
    
       public class LanguageRouteHandler : IRouteHandler
       {
           public IHttpHandler GetHttpHandler(RequestContext requestContext)
           {
               string page = CheckForNullValue(requestContext.RouteData.Values["page"]);
               string virtualPath = "~/" + page;
    
               if (string.IsNullOrEmpty(page))
               {
                   string language= CheckForNullValue(requestContext.RouteData.Values["language"]);
                   string newPage = "/home";
    
                   if (!string.IsNullOrEmpty(language))
                       newPage = "/" + language + newPage;
                   HttpContext.Current.Response.Redirect(newPage, false);
                   HttpContext.Current.Response.StatusCode = 301;
                   HttpContext.Current.Response.End();
    
                  //Otherwise, route to home
                  //page = "home";
               }
    
              if (!virtualPath.Contains(".aspx"))
                    virtualPath += ".aspx";
    
               try
               {
                   return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as IHttpHandler;
               }
               catch
               {
                   return null;
               }
           }
       }
    }
    

    请看这篇文章: http://dotnethints.com/blogs/localization-using-routing-system-on-a-web-forms-project-

    【讨论】:

    • 我认为这是asp.net项目中语言特定自定义路由的最佳解决方案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2013-04-03
    相关资源
    最近更新 更多