Skip to content

Instantly share code, notes, and snippets.

@AniAko
Forked from frankgeerlings/gist:1711560
Last active April 3, 2019 13:43
Show Gist options
  • Save AniAko/3f955b5ad9a71f17635e to your computer and use it in GitHub Desktop.
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.
// 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