[英]Need help in URL Routing in asp.net 4.0
for SEO friendly url I use routing functionality of ASP.Net 4.0. 对于SEO友好的URL,我使用ASP.Net 4.0的路由功能。
Anyone knows how to change route of URL www.mysite.com/ln=en-us to www.mysite.com/en-us/default.aspx ? 任何人都知道如何改变URL www.mysite.com/ln=en-us的路线www.mysite.com/en-us/default.aspx?
Thanks 谢谢
I assume you are working with web forms first creat this class: 我假设您正在使用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;
}
}
in your Global.asax add this: 在您的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.