2016-11-22 68 views
1

我試圖在創建的Web API中實現錯誤處理,需要以JSON格式返回異常詳細信息。我創建了BALExceptionFilterAttribute像在Web API中使用ExceptionFilterAttribute

public class BALExceptionFilterAttribute : ExceptionFilterAttribute 
{ 
    public override void OnException(HttpActionExecutedContext actionExecutedContext) 
    { 
     base.OnException(actionExecutedContext); 
     actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, new { error = actionExecutedContext.Exception.Message }); 
    } 
} 

而且在Gloal.asax.cs註冊他們像

GlobalConfiguration.Configuration.Filters.Add(new BALExceptionFilterAttribute()); 

在我的控制器,我想拋出像

[HttpGet] 
    [BALExceptionFilter] 
    public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT) 
    { 
     if (string.IsNullOrEmpty(ROOM) 
     { 
      return Request.CreateResponse(new { error = "Input paramete cannot be Empty or NULL" }); 
     } 
      //throws the exception 
      throw new BALExceptionFilterAttribute(); 

      List<OracleParameter> prms = new List<OracleParameter>(); 
      List<string> selectionStrings = new List<string>(); 
      prms.Add(new OracleParameter("ROOM", OracleDbType.Varchar2, ROOM, ParameterDirection.Input)); 
      prms.Add(new OracleParameter("DOB_GT", OracleDbType.Date, DOB_GT, ParameterDirection.Input)); 
      string connStr = ConfigurationManager.ConnectionStrings["TGSDataBaseConnection"].ConnectionString; 
      using (OracleConnection dbconn = new OracleConnection(connStr)) 
      { 
       DataSet userDataset = new DataSet(); 
       var strQuery = "SELECT * from LIMS_SAMPLE_RESULTS_VW where ROOM = :ROOM and DOB > :DOB_GT "; 
       var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) }; 
       var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json")); 
       ContentDispositionHeaderValue contentDisposition = null; 
       if (ContentDispositionHeaderValue.TryParse("inline; filename=TGSData.json", out contentDisposition)) 
       { 
        response.Content.Headers.ContentDisposition = contentDisposition; 
       } 
       return response; 
       } 
     } 

的例外,但它顯示的錯誤like on throw new BALExceptionFilterAttribute(); Error 1 The type caught or thrown must be derived from System.Exception

回答

2
//throws the exception 
throw new BALExceptionFilterAttribute(); 

這會產生編譯器錯誤。異常過濾器屬性是在發生異常時執行某些操作,以便以通用方式處理它,例如重定向到錯誤頁面或在json響應中發回一般異常消息等。異常過濾器屬性本身是不是一個例外,它處理異常。

因此throw new BALExceptionFilterAttribute();無效,因爲BALExceptionFilterAttribute也不例外。

如果你想要一個BALException類型,然後創建一個。

public class BALException : Exception { /* add properties and constructors */} 

,現在你可以把它

throw new BALException(); 

,然後你可以配置BALExceptionFilterAttribute做一下這個例外到達過濾器(控制器中沒有被捕獲)事件。

1

ExceptionFilterAttribute繼承自System.Attribute而不是System.Exception

MSDN Reference

爲什麼你沒有得到一個編譯器錯誤,我不是很確定。

編輯

一個過濾器,可以掛接到在不同點ASP.NET管道來編寫處理事件的代碼。異常處理就是一個很好的例子。

如果您還需要自定義異常,那麼您可以創建一個額外的類,例如public class BALException : Exception,儘管按照描述查看您的用例,您可能會使用現有的框架異常。

我完全清楚你爲什麼要把它放在正常執行你的工作流程中,如原文所寫。

+0

是的,它確實會引發編譯器錯誤。我該如何處理這個問題 – trx