【发布时间】:2016-11-29 00:50:29
【问题描述】:
有没有办法将所有以斜杠结尾的网址重定向到不带斜杠的网址?
【问题讨论】:
有没有办法将所有以斜杠结尾的网址重定向到不带斜杠的网址?
【问题讨论】:
在Global.asax 的Application_BeginRequest 方法中,添加以下内容:
if (HttpContext.Current.Request.Url.AbsolutePath != "/" && HttpContext.Current.Request.Url.AbsolutePath.EndsWith("/")) {
string redirect = HttpContext.Current.Request.Url.AbsolutePath;
redirect = redirect.Remove(redirect.Length - 1);
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", redirect);
Response.End();
}
【讨论】: