Solved

"Cannot redirect after HTTP headers have been sent."

Posted on 2011-03-15
4
5,631 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

789 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