您需要使用IRouteHandler并且需要创建自定义路由。
默认情况下,Web 窗体使用文件系统请求处理。例如,MyWebsite/contact.aspx请求搜索位于根菜单下
的contact.aspx文件。MyWebsite/superheroes/superheroes.aspx应该在根菜单下的 superheroes 文件中查找superheroes.aspx文件。
使用global.asax文件,您可以添加我们自己的处理。您只需要从Application_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;
}
}
}
}
请看这篇文章:
http ://dotnethints.com/blogs/localization-using-routing-system-on-a-web-forms-project-