需要asp.net 4.0中URL路由的帮助吗

需要asp.net 4.0中URL路由的帮助吗,asp.net,Asp.net,对于SEO友好的url,我使用ASP.NET4.0的路由功能 有人知道如何将URLwww.mysite.com/ln=en-us的路径更改为www.mysite.com/en-us/default.aspx 谢谢我想您正在使用web表单 首先创建这个类: public class LangRouteHandler : IRouteHandler { public System.Web.IHttpHandler GetHttpHandler(System.Web.Routing.Requ

对于SEO友好的url,我使用ASP.NET4.0的路由功能

有人知道如何将URLwww.mysite.com/ln=en-us的路径更改为www.mysite.com/en-us/default.aspx


谢谢

我想您正在使用web表单 首先创建这个类:

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