简体   繁体   中英

302 redirect still attempting to run original request

We have a section of code which detects old URL's and performs a redirect as follows:

Response.Clear();
Response.StatusCode = 301;
Response.Status = "301 Moved Permanently";
try
{
    Response.AddHeader("Location", MyLink.GetFullPath());
} catch
{
    Response.StatusCode = 302;
    Response.Status = "302 Moved Temporarily";
    Response.AddHeader("Location", "/");
}
Response.End();

What is essentially happening is that there is an attempt at a 301 permanent redirect and if this is not successful then a "302 Moved Temporarily" is thrown to the default home page.

PROBLEM: The problem I'm encountering is as follows. If a URL entered (lets say "/product/ABC123" for example). The URL 301 redirection fails and the 302 redirect in the catch is executed, however the original URL ("/product/ABC123") is still executed in the background. When I run the website in the debugger, the 302 redirect works fine and the default home page comes up and then half a second later a supplementary page is executed and comes up with a server 500 error due to the fact that the original URL is no longer acceptable.

My question is as follows: How can I completely stop the original request and just let the 302 redirect do what it needs to do. I have tried "HttpContext.ApplicationInstance.CompleteRequest();" beneath the "Response.End()" but it did not stop the original request.

EDIT: The redirection code is being executed from the Index method of a Controller class.

如果您处于控制器操作中,请退出操作方法并返回new HttpStatusCodeResult(302)

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