C# 如何在Asp.Net项目中创建特定于语言的Url?
我在项目中使用westwind全球化库来实现多种语言。一切正常。 唯一的一个问题是,我在url的末尾得到了区域性引用变量 我想在域名和页面名之间使用区域性引用变量C# 如何在Asp.Net项目中创建特定于语言的Url?,c#,asp.net,localization,url-routing,westwind-globalization,C#,Asp.net,Localization,Url Routing,Westwind Globalization,我在项目中使用westwind全球化库来实现多种语言。一切正常。 唯一的一个问题是,我在url的末尾得到了区域性引用变量 我想在域名和页面名之间使用区域性引用变量 实际上,我想像Microsoft网站那样做。您需要使用IRouteHandler,并且需要创建自定义路由 默认情况下,Web表单使用文件系统请求处理。例如,MyWebsite/contact.aspx请求搜索根菜单下的contact.aspx文件。 MyWebsite/superheros/superheros.aspx应在根菜单下的
实际上,我想像Microsoft网站那样做。您需要使用IRouteHandler,并且需要创建自定义路由 默认情况下,Web表单使用文件系统请求处理。例如,MyWebsite/contact.aspx请求搜索根菜单下的contact.aspx文件。 MyWebsite/superheros/superheros.aspx应在根菜单下的superheros文件中查找superheros.aspx文件 使用global.asax文件,您可以添加我们自己的处理。 您只需从应用程序\u Start方法中调用一个方法,该方法具有RouteCollection参数。 因此,您需要创建一个名为RegisterRoutes的方法,并将其放入名为MyRouteConfig.cs的新文件中 下面是一个例子:
using System;
using Microsoft.AspNet.FriendlyUrls;
using System.Web.Routing;
using System.Web;
using System.Web.UI;
using System.Web.Compilation;
public static class MyRouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
routes.MapPageRoute("superheroes", "superhero/{SuperheroName}", "~/superheroes.aspx");
routes.MapPageRoute("languageSuperheroes", "{language}/superhero/{SuperheroName}", "~/superheroes.aspx");
routes.Add(new System.Web.Routing.Route("{language}/{*page}", new LanguageRouteHandler()));
}
public class LanguageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string page = CheckForNullValue(requestContext.RouteData.Values["page"]);
string virtualPath = "~/" + page;
if (string.IsNullOrEmpty(page))
{
string language= CheckForNullValue(requestContext.RouteData.Values["language"]);
string newPage = "/home";
if (!string.IsNullOrEmpty(language))
newPage = "/" + language + newPage;
HttpContext.Current.Response.Redirect(newPage, false);
HttpContext.Current.Response.StatusCode = 301;
HttpContext.Current.Response.End();
//Otherwise, route to home
//page = "home";
}
if (!virtualPath.Contains(".aspx"))
virtualPath += ".aspx";
try
{
return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as IHttpHandler;
}
catch
{
return null;
}
}
}
}
请看一下这篇文章:
我认为这是asp.net项目中针对特定语言的自定义路由的最佳解决方案。非常感谢你。