Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting exception "Cannot redirect after HTTP headers have been sent" when redirecting to another page

Tags:

asp.net

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.

like image 834
nimi Avatar asked Apr 13 '11 09:04

nimi


People also ask

How do you fix Cannot redirect after HTTP headers have been sent?

If you are trying to redirect after the headers have been sent (if, for instance, you are doing an error redirect from a partially-generated page), you can send some client Javascript (location. replace or location. href, etc.) to redirect to whatever URL you want.

What happens to headers on redirect?

In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3 , and a Location header holding the URL to redirect to. When browsers receive a redirect, they immediately load the new URL provided in the Location header.

Can you add headers to a redirect?

It's impossible to redirect to a page with custom headers set, no matter what language or framework you use. In other words, there's no way to trigger an HTTP redirect and cause the client (browser) to add a custom header.


1 Answers

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.

like image 155
m.edmondson Avatar answered Sep 24 '22 08:09

m.edmondson