ASP.NET Web Application (.NET Framework) - How to remove returnurl from url?

Add this to your Global.asax file.


public class MvcApplication : HttpApplication {

  private const string ReturnUrlRegexPattern = @"\?ReturnUrl=.*$";

  public MvcApplication()
  {
    PreSendRequestHeaders += MvcApplicationOnPreSendRequestHeaders;
  }

  private void MvcApplicationOnPreSendRequestHeaders(object sender, EventArgs e)
  {
    string redirectUrl = Response.RedirectLocation;

    if (string.IsNullOrEmpty(redirectUrl) || !Regex.IsMatch(redirectUrl, ReturnUrlRegexPattern))
    {
      return;
    }

    Response.RedirectLocation = Regex.Replace(redirectUrl, ReturnUrlRegexPattern, string.Empty);
  }
}