clement
clement

Reputation: 4266

I can't resolve unhandeld exception: cannot set status after HTTP headers already set

I have a application that make warning in IIS.

When I tried it in my visual studio, nothing in error. I made a Application_Error in the global.asax to catch unhanded exceptions.

Here are informations about this error:

Message: Server cannot set status after HTTP headers have been sent.
Source: System.Web. 
InnerException:  (none)
End of stacktrace:

   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

How Can I debug it?

When the user land to the webapplication, if there is no session, he is redirected to a auth service that redirect too the user in the webapplication, with a token in URL to authenticater the user.

This is during this process that the error is thrown.

EDIT: maybe it's this code that generate warning

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string token = Request.QueryString["token"];
    // In the case I suspect to generate warning, the token is null!
    if (!string.IsNullOrEmpty(token))
    {
        SessionManager.IdentityToken = token;
        SessionManager.UserDatas.IdentityToken = token;
        SSOInformations sso = SSOManager.GetSSO(new Guid(token), false);
        if (sso != null)
        {
            SessionManager.UserDatas.loginID = sso.login;

            // Get and set session
            // Code

            catch (Exception ex)
            {
                TempData["ERROR_MESSAGE"] = ex.Message;
                RedirectToAction("index", "error");
            }
        }
        else
        { // if the sso failed, retry to authenticate
             Response.Redirect(ConfigManager.AuthService);
          // 31122013 : CHA : to avoid to write warnings on the server
              return;
        }
        //}
    }

    base.OnActionExecuting(filterContext);
}

Upvotes: 4

Views: 1628

Answers (1)

LostInComputer
LostInComputer

Reputation: 15410

The action method is still being triggered even when you call Response.Redirect and RedirectToAction.

To fix it, change the redirect lines to

filterContext.Result = RedirectToAction("index", "error");

and

filterContext.Result = Redirect(ConfigManager.AuthService);

Upvotes: 1

Related Questions