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.开发者_如何学JAVAAddHeader("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.
If you are in a controller action, exit the action method with returning a new HttpStatusCodeResult(302)
.
精彩评论