未触发异常过滤器
Exception filter not triggered
我想在工作中将 ExceptionFilter 添加到我的项目中。
所以我在 .net 4.5 上用 VS2013 做了一个测试 api 项目:
Web api 获取操作引发自定义异常(具有过滤器属性)
正在构建自定义异常构造函数
过滤异常捕获并处理。
效果很好!
现在,我转到我的工作项目,该项目使用 .net 4.5,创建了一个 TestController 文件并复制 粘贴我的测试项目文件中的代码。
Web Api 操作被命中,自定义异常构造函数被调用但异常过滤器没有被触发。为什么?
这是我的 ApiController、ExceptionFilter、CustomException 和一些 Customer class:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Services.Description;
namespace Web.Controllers.WebApi
{
public class TestController : ApiController
{
public static List<Customer> Customers;
static TestController()
{
Customers = new List<Customer>()
{
new Customer() {Id = 1, Name = "person1"},
new Customer() {Id = 2, Name = "person2"},
};
}
[ApiExceptionFilterAttribute]
public IHttpActionResult Get(int id)
{
if (Customers != null)
{
var customer = Customers.FirstOrDefault(x => x.Id == id);
if (customer == null)
{
throw new CustomerNotExistsException(id);
}
return Ok(customer);
}
return InternalServerError();
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is CustomerNotExistsException)
{
throw new HttpResponseException(actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ((CustomerNotExistsException)actionExecutedContext.Exception).Message));
}
else
{
//genenral 500 error
//log exception
}
}
}
public class CustomerNotExistsException : HttpResponseException
{
public int CustomerId { get; set; }
public string Message { get; set; }
private const string message = "customer id {0} not found";
public CustomerNotExistsException(int customerId) : base(new HttpResponseMessage())
{
CustomerId = customerId;
Message = string.Format(message, customerId);
}
public CustomerNotExistsException(int customerId, string message) : base(new HttpResponseMessage())
{
CustomerId = customerId;
Message = message;
}
}
}
这是我的网站Api配置:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
}
}
这是我的 Global.asax
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles);
}
void Application_Error(object sender, EventArgs e)
{
//some code here
}
}
更新:
刚刚意识到,当异常过滤器未被触发时,我得到状态码 200!
很奇怪,毕竟抛出异常
对我有用的解决方案是更改我创建的 CustomException(我的代码中的 CustomerNotExistsException)以继承自 Exception base class 而不是 HttpResponseException。
我想在工作中将 ExceptionFilter 添加到我的项目中。 所以我在 .net 4.5 上用 VS2013 做了一个测试 api 项目:
Web api 获取操作引发自定义异常(具有过滤器属性)
正在构建自定义异常构造函数
过滤异常捕获并处理。
效果很好!
现在,我转到我的工作项目,该项目使用 .net 4.5,创建了一个 TestController 文件并复制 粘贴我的测试项目文件中的代码。 Web Api 操作被命中,自定义异常构造函数被调用但异常过滤器没有被触发。为什么?
这是我的 ApiController、ExceptionFilter、CustomException 和一些 Customer class:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Services.Description;
namespace Web.Controllers.WebApi
{
public class TestController : ApiController
{
public static List<Customer> Customers;
static TestController()
{
Customers = new List<Customer>()
{
new Customer() {Id = 1, Name = "person1"},
new Customer() {Id = 2, Name = "person2"},
};
}
[ApiExceptionFilterAttribute]
public IHttpActionResult Get(int id)
{
if (Customers != null)
{
var customer = Customers.FirstOrDefault(x => x.Id == id);
if (customer == null)
{
throw new CustomerNotExistsException(id);
}
return Ok(customer);
}
return InternalServerError();
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is CustomerNotExistsException)
{
throw new HttpResponseException(actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ((CustomerNotExistsException)actionExecutedContext.Exception).Message));
}
else
{
//genenral 500 error
//log exception
}
}
}
public class CustomerNotExistsException : HttpResponseException
{
public int CustomerId { get; set; }
public string Message { get; set; }
private const string message = "customer id {0} not found";
public CustomerNotExistsException(int customerId) : base(new HttpResponseMessage())
{
CustomerId = customerId;
Message = string.Format(message, customerId);
}
public CustomerNotExistsException(int customerId, string message) : base(new HttpResponseMessage())
{
CustomerId = customerId;
Message = message;
}
}
}
这是我的网站Api配置:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
}
}
这是我的 Global.asax
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles);
}
void Application_Error(object sender, EventArgs e)
{
//some code here
}
}
更新:
刚刚意识到,当异常过滤器未被触发时,我得到状态码 200!
很奇怪,毕竟抛出异常
对我有用的解决方案是更改我创建的 CustomException(我的代码中的 CustomerNotExistsException)以继承自 Exception base class 而不是 HttpResponseException。