我无法解决unhandeld异常:在已设置HTTP标头后无法设置状态

时间:2014-01-10 11:35:47

标签: asp.net asp.net-mvc asp.net-mvc-4 exception-handling http-headers

我有一个应用程序在IIS中发出警告。

当我在visual studio中尝试时,没有任何错误。 我在Application_Error中设置了global.asax来抓住unhanded exceptions

以下是有关此错误的信息:

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)

我该如何调试?

当用户登陆到网络应用程序时,如果没有session,则他redirected是一个身份验证服务,redirect也是网络应用程序中的用户,网址中有一个令牌验证用户。

这是在此过程中抛出错误。

编辑:也许是这段代码产生了警告

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);
}

1 个答案:

答案 0 :(得分:1)

即使您拨打Response.RedirectRedirectToAction,操作方法仍会被触发。

要解决此问题,请将重定向行更改为

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

filterContext.Result = Redirect(ConfigManager.AuthService);