2015-07-11 53 views
2

我想添加ExceptionFilter到我的項目工作。 所以我在.NET 4.5做了一個試驗API項目與VS2013:異常過濾器沒有觸發

  1. 的Web API獲得動作拋出一個自定義異常(有一個過濾器的屬性)

  2. 自定義異常構造是建立

  3. 過濾器異常抓住它並處理。

它的工作很棒!現在

,我去了我的工作項目,這正與.NET 4.5,創建一個文件的TestController和複製粘貼將代碼從我的測試項目文件。 網絡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!

非常奇怪,因爲它拋出異常畢竟。

回答

0

我工作的解決方案是將我創建的CustomException(我的代碼中的CustomerNotExistsException)更改爲從Exception基類繼承,而不是從HttpResponseException繼承。