-
-
Save AniAko/3f955b5ad9a71f17635e to your computer and use it in GitHub Desktop.
An update on https://gist.github.com/frankgeerlings/1711560 MVC doesn't like the missing trailing slash on the URI authority (empty path), it's against HTTP/1.1 requirements as well it seems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Put this in Global.asax.cs | |
protected void Application_BeginRequest(object sender, EventArgs e) | |
{ | |
// Do Not Allow URL to end in trailing slash | |
string url = HttpContext.Current.Request.Url.AbsolutePath; | |
if (string.IsNullOrEmpty(url) | |
|| url.Trim() == "/" | |
|| url.Trim() == "\\") return; | |
string lastChar = url[url.Length - 1].ToString(); | |
if (lastChar == "/" || lastChar == "\\") | |
{ | |
url = url.Substring(0, url.Length - 1); | |
Response.Clear(); | |
Response.Status = "301 Moved Permanently"; | |
Response.AddHeader("Location", url); | |
Response.End(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment