未触发异常过滤器
本文关键字:过滤器 异常 | 更新日期: 2023-09-27 18:02:16
我想在我的工作项目中添加ExceptionFilter。所以我在。net 4.5上用VS2013做了一个测试api项目:
-
web api get动作抛出一个自定义异常(有一个filter属性)
-
正在构建自定义异常构造函数
效果很好!
现在,我去了我的工作项目,这是与。net 4.5工作,创建了一个TestController文件和复制粘贴从我的测试项目文件的代码。web Api操作被击中,自定义异常构造函数被调用,但异常过滤器没有被触发。为什么?这是我的ApiController, ExceptionFilter, CustomException和一些Customer类: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;
}
}
}
这是我的WebApiConfig:
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基类继承,而不是从HttpResponseException。