When I call Response.Redirect(someUrl)
I get an HttpException: "Cannot redirect after HTTP headers have been sent".
Why do I get this? And how can I fix this issue?
When I call Response.Redirect(someUrl)
I get an HttpException: "Cannot redirect after HTTP headers have been sent".
Why do I get this? And how can I fix this issue?
You can also use below mentioned code
Error Cannot redirect after HTTP headers have been sent.
System.Web.HttpException (0x80004005): Cannot redirect after HTTP headers have been sent.
Suggestion
If we use asp.net mvc and working on same controller and redirect to different Action then you do not need to write..
Response.Redirect("ActionName","ControllerName");
its better to use only
return RedirectToAction("ActionName");
or
return View("ViewName");
Once you send any content at all to the client, the HTTP headers have already been sent. A
Response.Redirect()
call works by sending special information in the headers that make the browser ask for a different URL.Since the headers were already sent, asp.net can't do what you want (modify the headers)
You can get around this by a) either doing the Redirect before you do anything else, or b) try using
Response.Buffer = true
before you do anything else, to make sure that no output is sent to the client until the whole page is done executing.