简体   繁体   中英

Removing Global.asax still executing code

I am running an asp.net web application and, for search engine optimisation, would like any domain name that I have bought to 301 redirect to my preferred domain name.

For testing purposes I added a Global.asax page to my localhost project and added the following code:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e) 
{ 
  string currentUrl = HttpContext.Current.Request.Url.ToString();
  if(!currentUrl.StartsWith("http://www.mydomain.org")) 
  { 
    string strStart = currentUrl.Substring(0, currentUrl.indexOf(@"/Pages"));
    Response.Status = "301 Moved Permanently"; 
    Response.AddHeader("Location", currentUrl.Replace(strStart, "http://www.mydomain.org")); 
    Response.End(); 
  } 
}

I replaced the "http://www.mydomain.org" with one of my current domain names. It all ran as I wanted it to. However, now when I remove that code from the .asax file, or even remove the asax file completely, that snippet of code still runs. So effectively running my localhost project opens my online website and I cannot stop this behaviour.

I have done the following:

  • Deleted EVERYTHING in the "Temporary ASP.NET Files" folder
  • Deleted the .dlls in my /bin folder and the entire /obj folder
  • Cleaned and rebuilt my solution.
  • Created an entirely new solution, then added files to it. This works at first but with the addition of the Global.asax file the same behaviour occurs.

Although this is the behaviour I eventually want, I would like to run, debug and edit it after my first implementation of it. I have seen the same problem before but it was seemingly fixed by one of the above methods. Please help!

Your problem has nothing to do with your code. It is caching of the 301 you are struggling with.

Caching of 301 can happen in many places along the way your request is going through, starting form your browser and the behavior can become unpredictable because of this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM