Solved

"Cannot redirect after HTTP headers have been sent."

Posted on 2011-03-15
4
5,155 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
Comment Utility
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
Comment Utility
it means that you have already written something to the output
0
 
LVL 29

Accepted Solution

by:
Randy Downs earned 250 total points
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

14 Experts available now in Live!

Get 1:1 Help Now