?
Solved

"Cannot redirect after HTTP headers have been sent."

Posted on 2011-03-15
4
Medium Priority
?
6,143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Assisted Solution

by:Roman Gherman
Roman Gherman earned 600 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 400 total points
ID: 35138819
it means that you have already written something to the output
0
 
LVL 30

Accepted Solution

by:
Randy Downs earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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