2016-09-02 43 views
0

這400錯誤的請求通過我的異常處理程序不走:400錯誤的請求不會通過自定義異常處理程序

{ 
    "message": "The request is invalid.", 
    "messageDetail": "The parameters dictionary contains a null entry for parameter 'subscriptionId' of non-nullable type 'System.Guid' for method 'System.Net.Http.HttpResponseMessage Get(System.Guid)' in 'Product.Controllers.ItemController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter." 
} 

在我WebApiConfig.cs我有這樣的:

config.Services.Replace(typeof(IExceptionHandler), new HttpExceptionHandler()); 

每一種異常的動作拋出,倉庫,服務層水平是由該異常處理程序處理。但對於這個錯誤,一般的WebAPI異常處理程序踢英寸

我應該如何解決這個問題,以便這個異常要經過我的異常處理程序?

回答

0

我有同樣的問題,在這裏我的解決辦法

1-添加過濾器

public class RequestNoValidoAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(HttpActionContext actionContext) 
    { 
     if (!actionContext.ModelState.IsValid) 
     { 
      var response = new HttpResponseMessage(HttpStatusCode.BadRequest) 
      { 
       RequestMessage = actionContext.Request, 
       Content = new StringContent("Formato no válido.") 
      }; 
      actionContext.Response = response; 
     } 
    } 
} 

2 - 添加過濾器在你的WebApiConfig.cs

config.Filters.Add(new RequestNoValidoAttribute()); 
相關問題