您好我在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方法中的数据集.
我做错了什么?
您必须将过滤器添加到全局过滤器并将响应设置为创建的响应实例:
actionExecutedContext.Response = actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.Accepted, new HttpError { { "Message" , "I am error"}, { "StatusCode" , "20"} });
请注意我在一段时间内提出的关于导致内存泄漏的非消耗响应的问题.因此,您需要先执行此操作:
if(actionExecutedContext.Response != null) actionExecutedContext.Response.Dispose();
然后
actionExecutedContext.Response = ...