2016-10-24 71 views
0

我正在使用自動生成的默認Web Api控制器。我正在測試客戶端的驗證和錯誤處理。但不知何故,我已經意識到,詳細錯誤消息不會傳遞給客戶端。要麼我拋出HttpResponseException或返回IHttpActionResult在這兩種情況下客戶端只看到「錯誤的請求」,而不是詳細的消息。任何人都可以解釋發生了什麼問題嗎?爲什麼詳細的錯誤消息不會傳遞給HttpClient?

 public IHttpActionResult Delete(int id) 
    { 
     if (id <= 0) 
     { 
      var response = new HttpResponseMessage(HttpStatusCode.NotFound) 
      { 
       Content = new StringContent("Id should be greater than zero.", System.Text.Encoding.UTF8, "text/plain"), 
       StatusCode = HttpStatusCode.NotFound 

      }; 
      throw new HttpResponseException(response) // Either this way 
     } 

     var itemToDelete = (from i in Values 
          where i.Id == id 
          select i).SingleOrDefault(); 

     if (itemToDelete == null) 
     { 
      return BadRequest(string.Format("Unable to find a value for the Id {0}", id)); // Or This way 
     } 

     Values.Remove(itemToDelete); 
     return Ok(); 
    } 

客戶端代碼爲:上述工程的

private async static Task DeleteValue(int id) 
    { 

     var url = "http://localhost:13628/api/Values/" + id; 
     using (var client = new HttpClient()) 
     { 
      var response = await client.DeleteAsync(url); 
      if (response.IsSuccessStatusCode) 
      { 
       await ReadValues(); 
      } 
      else 
      { 
       Console.WriteLine(response.ReasonPhrase); 
       Console.WriteLine(response.StatusCode); 
      } 
     } 
    } 

無??

Thx

+0

你在'response'中收到了一條消息嗎? –

+0

*通常*,暴露您的Web服務器的內部工作對良性用戶沒有任何用處,但可能會將惡意暴露給惡意用戶。因此,*默認*傾向於隱藏詳細信息。 –

回答

0

將以下代碼替換爲Web API刪除操作。使用HttpResponseMessage作爲回報類不用於API,而不是IHttpActionResult

 [HttpDelete] 
     [Route("{id:int:min(1)}")] 
     public async Task<HttpResponseMessage> DeleteAsync(int id) 
     { 
      if(id < 0) 
      { 
       return await Task.FromResult<HttpResponseMessage>(Request.CreateResponse<string>(HttpStatusCode.BadRequest, "Id should be greater than zero.")); 
      } 
      try 
      { 
       var itemToDelete = (from i in Values 
            where i.Id == id 
            select i).SingleOrDefault(); 

       if (itemToDelete == null) 
       { 
        return await Task.FromResult<HttpResponseMessage>(Request.CreateResponse<string>(HttpStatusCode.NotFound, 
             string.Format("Unable to find a value for the Id {0}", id))); 
       } 

       Values.Remove(itemToDelete); 
       return await Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.OK)); 
      } 
      catch (Exception ex) 
      { 
       return Request.CreateResponse<string>(HttpStatusCode.InternalServerError, "Something went wrong."); // Default message if exception occured 
      } 
     } 

和客戶端:

 private async static Task DeleteValue(int id) 
     { 

      var url = "http://localhost:13628/api/Values/" + id; 
      using (var client = new HttpClient()) 
      { 
       var response = await client.DeleteAsync(url); 
       if (response.IsSuccessStatusCode) 
       { 
        await ReadValues(); 
       } 
       else 
       { 
        var errorMessage = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()); 
        // Here Newtonsoft.Json Package is used to deserialize response content 
        Console.WriteLine(errorMessage); 
        Console.WriteLine(response.StatusCode); 
       } 
      } 
     } 

上面的代碼是工作在我身邊。

0

在您的客戶端變化Console.WriteLine(response.ReasonPhrase);

Console.WriteLine(response.Content.ReadAsStringAsync().Result);

,它會給詳細的錯誤信息。

相關問題