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

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

当我在我的视觉工作室尝试时,没有任何错误.
我在global.asax中创建了一个Application_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)

我该如何调试?

用户登陆到web应用程序时,如果没有会话,则会将其重定向到auth服务,该服务在web应用程序中重定向用户,并在URL中使用令牌来验证用户.

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

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

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

解决方法

即使您调用Response.Redirect和RedirectToAction,仍会触发操作方法.

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

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

filterContext.Result = Redirect(ConfigManager.AuthService);

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....