mvc - server cannot set status after http headers have been sent

7691
Views
1
Answers

Hi ,
I have a MVC application which throws this wired error sometimes "
Here is the full error

         Event Type:        Error
           
Event Source:    ASP.NET 2.0.50727.0
           
Event Category:                None
           
Event ID:              7777
           
Date:                     12/28/2011
           
Time:                     12:00:42 PM
           
User:                     N/A
           
Computer:          Webserver
           
Description:
           
The description for Event ID ( 7777 ) in Source ( ASP.NET 2.0.50727.0 ) cannot be found.
   
The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer.
     
You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details.
The following information is part of the event: MESSAGE: Server cannot set status after HTTP headers have been sent.
            SOURCE
: System.Web
            FORM
:
            QUERYSTRING
:
            TARGETSITE
: Void set_StatusCode(Int32)
            STACKTRACE
:    at System.Web.HttpResponse.set_StatusCode(Int32 value)
               at
Pha.Mvc.Web.StaticFileHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) in C:\Web\App\StaticFileHandler.cs:line 128
           at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
           at
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously).
edit | flag
William Patrick
Asked on: Dec 30, 2011 at 2:52AM

1 Answers

91
8
91

The Possible causes may be

  1. your action was executing, writing markup to response stream
  2. the stream was unbuffered forcing the response headers to get written before the markup writing could begin.
  3. Your view encountered a runtime error
  4. Exception handler kicks in trying to set the status code to something else non-200
  5. Fails because the headers have already been sent.

If you could still encounter runtime errors in View binding e.g. null reference exceptions.

A better solution for this is to ensure that

Response.BufferOutput = true;

before any bytes are sent to the Response stream. e.g. in your controller action or On_Begin_Request in application. This enables server transfers, cookies/headers to be set etc. right the way up to naturally ending response, or calling end/flush.

edit | flag
Gokul A
Answered on: Dec 30, 2011 at 2:56AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags