【问题标题】:Need help in URL Routing in asp.net 4.0在 asp.net 4.0 中需要 URL 路由方面的帮助
【发布时间】:2012-05-08 22:29:14
【问题描述】:

对于 SEO 友好的 url,我使用 ASP.Net 4.0 的路由功能。

任何人都知道如何将 URL www.mysite.com/ln=en-us 的路由更改为 www.mysite.com/en-us/default.aspx ?

谢谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    我假设您正在使用网络表单 首先创建这个类:

    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;
    
        }
    }
    

    在您的 Global.asax 中添加:

    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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多