2012-05-08 22 views
0

SEOに優しいURL私はASP.Net 4.0のルーティング機能を使用しています。asp.net 4.0のURLルーティングのヘルプが必要

誰もがwww.mysite.com/en-us/default.aspxにURL www.mysite.com/ln=en-usのルートを変更する方法を知っていますか?私はあなたがウェブで作業していると仮定し

おかげ

+0

はそれがMVCまたはWebフォームです? –

答えて

0

は 最初のレコード生成、このクラス構成:あなたのGlobal.asaxの中

public class LangRouteHandler : IRouteHandler 
{ 

    public System.Web.IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext) 
    { 

     //Fill the context with the route data, just in case some page needs it 
     foreach (var value_loopVariable in requestContext.RouteData.Values) 
     { 
      var value = value_loopVariable; 
      HttpContext.Current.Items[value.Key] = value.Value; 
     } 

     string VirtualPath = null; 
     VirtualPath = "~/" + requestContext.RouteData.Values["page"];// +".aspx"; 

     IHttpHandler redirectPage = default(IHttpHandler); 

     redirectPage = (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)); 
     return redirectPage; 

    } 
} 

これを追加します。

public void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     RegisterRoutes(RouteTable.Routes); 
    } 


    public void RegisterRoutes(RouteCollection routes) 
    { 
     Route reportRoute = default(Route); 
     string DefaultLang = "es"; 

     reportRoute = new Route("{lang}/{page}", new LangRouteHandler()); 
     //* if you want, you can contrain the values 
     //reportRoute.Constraints = New RouteValueDictionary(New With {.lang = "[a-z]{2}"}) 
     reportRoute.Defaults = new RouteValueDictionary(new 
     { 
      lang = DefaultLang, 
      page = "home" 
     }); 

     routes.Add(reportRoute); 
    } 
関連する問題