Solved

"Cannot redirect after HTTP headers have been sent."

Posted on 2011-03-15
4
5,410 Views
Last Modified: 2012-05-11
I am trying to redirect from an ASP.NET MVC Controller Action to a static domain address. But I get the followinng exception "Cannot redirect after HTTP headers have been sent."

Any suggestions?

newbieweb
public ActionResult DataAdmin()
        {
            return Redirect("http://www.google.com");
        }

Open in new window

0
Comment
Question by:newbieweb
4 Comments
 
LVL 9

Assisted Solution

by:Roman Gherman
Roman Gherman earned 150 total points
ID: 35138816
Hi,

It looks like your headers have been set already in another place.

Try calling Response.Headers.Clear() before the Redirect to see if that helps

Find me on: www.extremedev.blogspot.com
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 100 total points
ID: 35138819
it means that you have already written something to the output
0
 
LVL 29

Accepted Solution

by:
Randy Downs earned 250 total points
ID: 35138837
Try this - http://stackoverflow.com/questions/159523/why-do-i-get-cannot-redirect-after-http-headers-have-been-sent-when-i-call-resp

According to the MSDN documentation for Response.Redirect(string url), it will throw an HttpException when "a redirection is attempted after the HTTP headers have been sent". Since Response.Redirect(string url) uses the Http "Location" response header (http://en.wikipedia.org/wiki/HTTP_headers#Responses), calling it will cause the headers to be sent to the client. This means that if you call it a second time, or if you call it after you've caused the headers to be sent in some other way, you'll get the HttpException.

One way to guard against calling Response.Redirect() multiple times is to check the Response.IsRequestBeingRedirected property (bool) before calling it.

// Causes headers to be sent to the client (Http "Location" response header) Response.Redirect("http://www.stackoverflow.com"); if (!Response.IsRequestBeingRedirected)         // Will not be called         Response.Redirect("http://www.google.com");
0
 

Author Closing Comment

by:newbieweb
ID: 35138908
Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now