9

I am getting exception

"Cannot redirect after HTTP headers have been sent." 

when on doing Response.Redirect("Home.aspx").

How can i solve this? I tried giving Response.Flush() before redirecting.

Anil
  • 3,722
  • 2
  • 24
  • 49
nimi
  • 5,359
  • 16
  • 58
  • 90
  • 1
    possible duplicate of [Why do I get "Cannot redirect after HTTP headers have been sent" when I call Response.Redirect(url)?](http://stackoverflow.com/questions/159523/why-do-i-get-cannot-redirect-after-http-headers-have-been-sent-when-i-call-resp) – Damien_The_Unbeliever Apr 13 '11 at 09:07
  • If the answers in the other question aren't helping you (e.g. the second highest voted one mentions making sure `Response.Buffer` is set true before anything else happens), then you're going to have to show us more of your code if you expect us to diagnose it. – Damien_The_Unbeliever Apr 13 '11 at 10:46
  • 1
    Duplicate to question and resolution found here: http://stackoverflow.com/questions/159523/why-do-i-get-cannot-redirect-after-http-headers-have-been-sent-when-i-call-resp – Chris Dixon Apr 13 '11 at 09:05
  • i tried the solution from the above thread. I am not able to fix the issue... Title of my tread may be duplicate, but the issue causing might be some other.... – nimi Apr 13 '11 at 09:35
  • I have a page which inherits from a base page where which checks whether the user has the permission to access the page. if the user has the permission then would be redirecting to the requested page, if not redirecting the user to access denied page. this check happens on the OnInt Method of the BasePage. – nimi Apr 13 '11 at 09:38
  • Just a thought, it could be to do with the hierarchy of calls - have you tried changing OnInit to OnLoad? – Chris Dixon Apr 13 '11 at 09:40
  • Possible duplicate of [Why do I get "Cannot redirect after HTTP headers have been sent" when I call Response.Redirect()?](http://stackoverflow.com/questions/159523/why-do-i-get-cannot-redirect-after-http-headers-have-been-sent-when-i-call-res) – Michael Freidgeim Mar 22 '17 at 06:31

3 Answers3

11

The problem is the Response.Flush() prior to redirecting. With HTTP you get one response for one request. Your browser requested the page only once, and I suspect you're trying to respond twice:

Response.Flush(); //First Response
Response.Redirect("Home.aspx"); //Second Response

Therefore taking this out the Response.Flush() will solve your problem.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
0

In my case, cause of the problem is that loading data in the scroll gridview is taking a long time. And before gridview data is not loaded exactly, but I press the redirect button.

Trying solve problem may be:

+lessen your data get

or

+before loading completion prevent to press redirect button

elifekiz
  • 1,456
  • 13
  • 26
-3

I just ran across this problem in a little different way. I had two Response.Redirect calls that were not wrapped in isolated branches of code. Worked fine in Chrome, but IE did not like it. It was an easy fix since I had two "if" statements. I just turned the second one into an "else if" like so...

if (QueryString("reset") == "1")
{
    // User is resetting password
    Response.Redirect("/Account/ResetPassword.aspx");
}
else if (DataUtils.IsInt(QueryString("id")))
{
    // User is authenticating...completing signup
    Response.Redirect("/Account/Activate.aspx");
}