你需要这个包:ASP.NET Friendly URL's如何实现可以在Hanselman's Blog找到
将此添加到您的 global.asax 文件中
void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
在 Global.asax 文件顶部添加引用,如下所示:
using System.Web.Routing;
这会自动重写您的网址。
如果要保留 default.aspx,请使用 URL 映射:
<urlMappings enabled="true">
<add url="~/Virtual-Page" mappedUrl="~/default.aspx" />
</urlMappings>
添加重写规则以重定向您的基本 URL,使用 URL Rewrite
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to Virtual-Page">
<match url="(.*)" />
<action type="Redirect" url="~/Virtual-Page" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
如果您不能使用 URL 重写,请在 global.asax 开始请求中添加一个 response.redirect:
void Application_BeginRequest(object sender, EventArgs e)
if (!HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://localhost/Virtual-Page"))
{
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString().ToLower().Replace("http://localhost/", "http://localhost/Virtual-Page"));
}