2016-09-20 11 views
3

以下の例外契約があります。HTTPクライアント経由でWeb APIからビジネス例外を投げる方法

public class AdminBusinessException : Exception 
{ 
    /// <summary> 
    /// Gets or Sets Exception Code. 
    /// </summary> 
    public string ExceptionCode { get; set; } 

    /// <summary> 
    /// Gets or Sets Description. 
    /// </summary> 
    public string Description { get; set; } 

    /// <summary> 
    /// Initiates here. 
    /// </summary> 
    /// <param name="exceptionCode"></param> 
    /// <param name="description"></param> 
    public AdminBusinessException(string exceptionCode, string description) 
    { 
     this.ExceptionCode = exceptionCode; 
     this.Description = description; 
    } 
} 

だから私は、例外コードだけでなく、私のHTTPクライアントに説明をスローします。 いずれか1つ私にこれを助けてください。

私はこれにstrucking午前:(:(

+0

可能な複製の[Web APIをクライアントに戻るカスタム例外をスロー](http://stackoverflow.com/questions/26718868/web-api-throw-custom-exception-back-to-client) –

答えて

0

あなたのWeb APIはHTTPの規則を使用して応答を処理する必要がありますし、あなたが身体にメッセージを含めることができ

例:。

Public IHttpActionResult DoSomethingBusiness(int id) 
{ 
    if(some error from client) 
     return BadRequest("some error message here") 

    return Ok("Everything seems good") 
} 
+0

例外をスローする方法はありますかオブジェクトをクライアントに送信するかどうかを指定します。 –

+0

例外オブジェクトとは、サーバーこの場合、500のようなステータスコードを返さなければならず、何が起こったのかを示すエラーメッセージが含まれていなければなりません。 エラーがクライアントデータの問題によるものだった場合、(Bad Request)を返し、例外の詳細をメッセージ本文に含める必要があります。 戻り値BadRequest(exception.ToString()) –

関連する問題