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'。 –