Stack,Owin WebApi服务忽略ExceptionFilter
由于某些原因,我的Owin WebApi服务忽略了我们的自定义异常处理程序。我正在关注asp.net exception handling的文档。以下是简化的实施细节(清理出商业专有内容)。
你能指出我忽略了什么吗?
自定义异常过滤器:
public class CustomExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
actionExecutedContext.Response.StatusCode = HttpStatusCode.NotFound;
actionExecutedContext.Response.Content = new StringContent("...my custom business...message...here...");
}
}
在启动过程中:
var filter = new CustomExceptionFilter();
config.Filters.Add(filter);
appBuilder.UseWebApi(config);
测试控制器:
[CustomExceptionFilter]
public class TestController: ApiController
{
public void Get()
{
throw new Exception(); // This is a simplification.
// This is really thrown in buried
// under layers of implementation details
// used by the controller.
}
}
我有一个项目,做这个确切的模式,除了在OnException修改响应我抛出新的HttpResponseException(new HttpResponseMessage(...'而不是修改'actionExecutedContext'。 –