CreateErrorResponse不会覆盖异常堆栈跟踪和消息

 e1985522z 发布于 2023-02-08 12:29

您好我在asp.net WebApi中创建一个异常处理机制,我对如何创建返回的响应消息感到困惑.这是我的代码:

public class ExceptionHandlerAttribute : ExceptionFilterAttribute  
{
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.Accepted, new HttpError
        {
            { "Message" , "I am error"},
            { "StatusCode" , "20"}
        });
    }
}

     [AllowAnonymous]
    [HttpPost]
    [Validate]
    public IHttpActionResult Register(UserModel userRegistrationModel)
    {
        throw new AccessDeniedException("message" , new Exception("inner exception"));
    }

 public class AccessDeniedException : BaseException
{
    public AccessDeniedException(string message, Exception exception)
        : base(message, exception)
    {

    }
}

  public class BaseException : Exception
{
    public BaseException(string message , Exception exception) : base(message , exception)
    {

    }
}

现在我已经在myfilter配置中注册了ExceptionHandlerAttributed,并且在调试时我意识到OnException方法中的代码运行.

问题是响应是这样的:

"readyState": 4,
"responseText": "{\"message\":\"An error has occurred.\",\"exceptionMessage\":\"message\",\"exceptionType\":\"CodeArt.Common.Exceptions.AccessDeniedException\",\"stackTrace\":\"   at System.Web.Http.ApiController.d__1.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\\r\\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()\",\"innerException\":{\"message\":\"An error has occurred.\",\"exceptionMessage\":\"inner exception\",\"exceptionType\":\"System.Exception\",\"stackTrace\":null}}",
"responseJSON": {
    "message": "An error has occurred.",
    "exceptionMessage": "message",
    "exceptionType": "CodeArt.Common.Exceptions.AccessDeniedException",
    "stackTrace": "   at System.Web.Http.ApiController.d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()",
    "innerException": {
        "message": "An error has occurred.",
        "exceptionMessage": "inner exception",
        "exceptionType": "System.Exception",
        "stackTrace": null
    }
},
"status": 500,
"statusText": "Internal Server Error"

我没有发送异常堆栈跟踪以及与异常相关的任何其他内容.我想发送的是OnException方法中的数据集.

我做错了什么?

1 个回答
  • 您必须将过滤器添加到全局过滤器并将响应设置为创建的响应实例:

    actionExecutedContext.Response = actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.Accepted, new HttpError
        {
            { "Message" , "I am error"},
            { "StatusCode" , "20"}
        });
    

    请注意我在一段时间内提出的关于导致内存泄漏的非消耗响应的问题.因此,您需要先执行此操作:

    if(actionExecutedContext.Response != null)
        actionExecutedContext.Response.Dispose();
    

    然后

    actionExecutedContext.Response =  ...
    

    2023-02-08 12:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有