2016-11-16 155 views
0

我正在創建一個ASP.NET Web API方法。 Web方法接受四個輸入參數,這些參數用於查詢Oracle數據庫並以JSON格式返回結果。輸入參數的類型是字符串和DateTime。 API調用看起來像?id=123&date_in=01-JAN-16。在控制器中,我必須處理驗證錯誤,例如API調用中id爲null或日期格式與dd-MMM-yy不同,並返回相應的錯誤消息。處理錯誤ASP.NET Web API

public class DataController : ApiController 
{ 
    [HttpGet] 
    public HttpResponseMessage Getdetails(string id,DateTime date_in) 
     { 
      List<OracleParameter> prms = new List<OracleParameter>(); 
      prms.Add(new OracleParameter("id", OracleDbType.Varchar2, id, ParameterDirection.Input)); 
      prms.Add(new OracleParameter("date_in", OracleDbType.Date, date_in, ParameterDirection.Input)); 
      string connStr = ConfigurationManager.ConnectionStrings["DtConnection"].ConnectionString; 
     using (OracleConnection dbconn = new OracleConnection(connStr)) 
     { 
      DataSet userDataset = new DataSet(); 
      var strQuery = "SELECT * from SAMPLE where id = :id and date_in = :date_in "; 
      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; 

我應該創建一個不同的類來處理這些異常嗎?我應該如何回覆答覆?

+0

您可以使用輸入參數,防止了該ID的null檢查:Getdetails(INT ID,日期時間date_in),這會從ASP.NET如果他們試圖做類似返回一個錯誤ID =火烈鳥&date_in = 01-JAN-16。至於錯誤處理,其中一種更簡單的方法可能是創建一個DelegatingHandler,它將在try/catch中調用SendAsync並在拋出異常時返回一致的錯誤消息。 –

回答

0

試試下面的代碼

public class DataController : ApiController 
      { 
       [HttpGet] 
       public HttpResponseMessage Getdetails(string id,DateTime date_in) 
       { 
        if(id==string.Empty || id==null) 
        { 
         return "Id Value Should not Empty or Null"; 
        } 
        if(!Regex.IsMatch(date_in, "^(([0-9])|([0-2][0-9])|([3][0- 
        1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\- 
        \d{4}$")) 
        { 
         return "Invalid Date Format"; 
        } 
        List<OracleParameter> prms = new List<OracleParameter>(); 
        prms.Add(new OracleParameter("id", OracleDbType.Varchar2, 
        id, ParameterDirection.Input)); 
        prms.Add(new OracleParameter("date_in", OracleDbType.Date, date_in, ParameterDirection.Input)); 
         string connStr = ConfigurationManager.ConnectionStrings["DtConnection"].ConnectionString; 
        using (OracleConnection dbconn = new OracleConnection(connStr)) 
        { 
         DataSet userDataset = new DataSet(); 
         var strQuery = "SELECT * from SAMPLE where id = :id and date_in = :date_in "; 
         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; 
      } 
      } 
+0

如果無法驗證。 嘗試將參數數據類型DateTime替換爲String DataType。 –

0

我們可以擴展ExceptionFilterAttribute並創建一個自定義異常過濾屬性。

public class CustomExceptionFilterAttribute : ExceptionFilterAttribute 
{ 
    public override void OnException(HttpActionExecutedContext context) 
    { 
     var exception = context.Exception; 
     context.Response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError); 
     context.Response.Content = new StringContent(exception.Message); 
    } 
} 

並在ApiController中使用它來返回vliadation消息。

[HttpGet] 
[CustomExceptionFilterAttribute] 
public HttpResponseMessage Getdetails(string id, DateTime date_in) 
{ 
     if (string.IsNullOrEmpty(id)) 
     { 
      throw new ArgumentNullException("id"); 
     } 
     //... 
}