【发布时间】:2014-04-07 07:07:42
【问题描述】:
我在我的 ASP.NET 4.0 项目中使用 URL 重写来实现 URL 路由。这是来自我的 Global.asax 的示例代码:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("main", "Main", "~/Home.aspx");
routes.MapPageRoute("aboutUs", "About-Us", "~/AboutUs.aspx");
...//so on and so forth
}
我还将它添加到 Application_BeginRequest 以将 Home.aspx 转发到同一 Global.asax 中的 Main:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.AbsolutePath.Contains("Home.aspx"))
{
HttpContext.Current.Response.Redirect("~/Main");
}
}
这样,任何尝试转到“Home.aspx”的请求都将被路由到“Main”,这仅在第一次加载时发生。路由适用于我项目中的所有页面。
现在,当我将代码发布到我的网站并启动网站时,会出现以下 URL:
www.MyWebSite.com/Main
如何在 URL 的末尾隐藏“Main”并使其看起来像这样:
www.MyWebSite.com
~谢谢
【问题讨论】: