0

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

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

谢谢

4

1 回答 1

0

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

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);
    }
于 2012-05-08T14:29:14.257 回答