如何从网站中的每个.aspx网页上删除.aspx?以下工作,但仅适用于网站的根目录,并定义长文件夹结构是低效和混乱的。
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("RemASPx", "{file}", "~/{file}.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
以下工作没有尾部斜杠,但我如何强制它有一个尾部斜杠,因为没有任何东西可以跟随catch-all routeURL?
routes.MapPageRoute("RemASPx", "{*file}", "~/{file}.aspx");
答案 0 :(得分:3)
保留此路由设置:
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("RemoveASPx", "{*file}", "~/{file}.aspx");
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
添加此重写:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsolutePath;
if (string.IsNullOrEmpty(url) ||
System.IO.Directory.Exists(Server.MapPath(url)))
return;
if (url.EndsWith("/"))
{
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url.Substring(0, url.Length - 1));
Response.End();
}
}
以上代码块: