【问题标题】:I can't resolve unhandeld exception: cannot set status after HTTP headers already set我无法解决未处理的异常:HTTP 标头已设置后无法设置状态
【发布时间】:2014-01-29 08:48:34
【问题描述】:

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

当我在visual studio 中尝试时,没有任何错误。 我在global.asax 中创建了一个Application_Error 来捕捉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)

如何调试它?

当用户登陆webapplication时,如果没有session,他就是redirected到一个身份验证服务,redirect也是webapplication中的用户,在URL中带有一个token来验证用户。

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

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

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

【问题讨论】:

  • 请发布引发错误的实际代码。
  • 这通常是由于在返回之前手动调用 HttpContext.Current.Response.End() 或 HttpContext.Current.Response.Redirect() 引起的。尝试将您的重定向放入操作中的 return Redirect()
  • @Alexander :好的,谢谢,我已经添加了我希望会产生错误的代码段!
  • @WeTTTT :好的,谢谢,但由于方法的定义,这是不可能的 (void OnActionExecuting(...))

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


【解决方案1】:

即使你调用Response.Redirect和RedirectToAction,action方法仍然被触发。

要修复它,请将重定向行更改为

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

和

filterContext.Result = Redirect(ConfigManager.AuthService);

【讨论】:

  • 好的,谢谢它看起来解决了我的部分问题。仍然有错误,但我做了一个很大的 IF 子句来直接重定向应用程序!谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 2011-01-23
  • 2015-11-07
  • 1970-01-01
相关资源
最近更新 更多