Share via


Server cannot set status after HTTP headers have been sent

Question

Sunday, June 29, 2014 4:31 AM

Hi

I used a handler in a project in VS2012 to send images to client and It worked. Now I uses it in VS2013 with some modifications but there is the error in the subject. Can anyone help? The error page follows:

Server Error in '/' Application.


Server cannot set status after HTTP headers have been sent.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Server cannot set status after HTTP headers have been sent.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): Server cannot set status after HTTP headers have been sent.]
   System.Web.HttpResponse.set_StatusCode(Int32 value) +9570808
   System.Web.HttpResponseWrapper.set_StatusCode(Int32 value) +9
   Microsoft.Owin.Host.SystemWeb.OwinCallContext.Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.IPropertySource.SetResponseStatusCode(Int32 value) +42
   Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.set_ResponseStatusCode(Int32 value) +40
   Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.PropertiesTrySetValue(String key, Object value) +1929
   Microsoft.Owin.Host.SystemWeb.CallEnvironment.AspNetDictionary.System.Collections.Generic.IDictionary<System.String,System.Object>.set_Item(String key, Object value) +45
   Microsoft.Owin.OwinResponse.Set(String key, T value) +85
   Microsoft.Owin.OwinResponse.set_StatusCode(Int32 value) +66
   Microsoft.Owin.OwinResponse.Redirect(String location) +46
   Microsoft.Owin.Security.Cookies.DefaultBehavior.<.cctor>b__2(CookieApplyRedirectContext context) +392
   Microsoft.Owin.Security.Cookies.CookieAuthenticationProvider.ApplyRedirect(CookieApplyRedirectContext context) +56
   Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler.ApplyResponseChallengeAsync() +1148
   Microsoft.Owin.Security.Infrastructure.<ApplyResponseCoreAsync>d__8.MoveNext() +286
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   Microsoft.Owin.Security.Infrastructure.<TeardownAsync>d__5.MoveNext() +266
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +1173
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +641
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +641
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22
   Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +33
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +150
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +42
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18447

All replies (9)

Monday, June 30, 2014 10:05 PM

Hi,

For this situation, about the error, "Server cannot set status after HTTP headers have been sent.", you could refer to the following information:

http://stackoverflow.com/questions/2383169/server-cannot-set-status-after-http-headers-have-been-sent-iis7-5

http://quysnhat.wordpress.com/2012/07/20/exception-message-server-cannot-set-status-after-http-headers-have-been-sent-when-generate-pdf-file-in-asp-net-mvc3/

Regards


Tuesday, July 1, 2014 1:35 AM

Hi

I searched for this error and tested many cases that caused this error but there was no change. When I started the next day It worked. I changed all of changes to previous state but it still worked!!! This makes me happy and at the same time frightened. I work with Web Forms. Thank you anyway.

Regards


Saturday, July 19, 2014 11:02 AM

Hi

I found the reason behind this error. I my handler I send the following HTTP codes based on the situation:

400, Bad Request

401, Unauthorized

403, Forbidden

404, Not Found

304, Not Modified

200, OK

All of them produces the expected result except 401, Unauthorized. When I set the StatusCode and StatusDescription to these values, there is this server error. I guess it interfere with ASP.NET authorization. Now there is the following question:

1-What is the real reason?

2-Can 401 code can be used in handlers?

Thanks


Friday, August 22, 2014 3:03 PM

I'm also looking for a solution to this.  I'm using an Authorization filter to lock down specific controllers and it looks like Owin for some reason is conflicting with this. When authorization fails and I set the status code to 401, I get this error.  THis just started happening after upgading to Microsoft.AspNet.Identity.Owin version="2.1.0

Any ideas?


Friday, August 22, 2014 3:09 PM

I also submitted issue here...

https://katanaproject.codeplex.com/workitem/341


Friday, August 22, 2014 3:59 PM

app.UseCookieAuthentication(New CookieAuthenticationOptions() With
{
.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
})

' removing this line from the above code fixed the issue.  I don't need owin to redirect when i get a 401.
',.LoginPath = New PathString("/Connector/PopUp")


Wednesday, February 8, 2017 9:03 AM

Hi, 

i am also getting the same issue but this issue is coming in live website only.on my local server i am not finding any issue.can any body help me what are the preferred reasons of this issue and how  can i solve this.

     i am using mvc 3 with orchard solution.


Tuesday, May 2, 2017 7:24 AM

try to use RedirectResult() method to redirect to a url instead of Response.Redirect() .like below example.

string url =   HttpContext.Request.Url.AbsoluteUri.Replace(HttpContext.Request.Url.AbsolutePath, "/Index.aspx");
               filterContext.Result = new RedirectResult(url);

above Index.aspx you can change whatever you want to redirect page.

Monday, January 28, 2019 5:45 PM

Thanks. This did the magic instead of a Respose.Redirect:

public ActionResult SearchResult()
{
return new RedirectResult(Request["selection"].ToString());
}