2014-12-04 208 views
0

我正在Web Api中使用GET動詞製作應用程序。 在關注GET的過程中,我調用了一個程序來檢查發送到基地的註冊命令。 根據條件的狀態,發送命令或沒有,但我需要在HTTP響應200 OK的身體負荷,一條消息,如果我用web api返回響應消息200 OK

return "This command is null or error";

與Chrome的郵差測試到我這裏來在體內的響應:

,而不必

「此命令爲空或錯誤」獲得報價,爲此,通過例外將響應消息HTTP 200 OK更改爲OK。

的代碼如下:

private string ComandosAEnviar(int pPuntoMedicionId) 
{ 
    MonitoreoEntities _context = new MonitoreoEntities(); 
    Comandos _comand = _context.Comandos.FirstOrDefault(a => a.ID_Destino == pPuntoMedicionId 
                   && a.Fecha_envio.HasValue == false); // a.ID_Destino == _ptomedicion && a.Fecha_envio.HasValue == false 

    if (_comand == null) //SUCCESS 
    { 
     HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK); 
     message.Content = new StringContent("This command is null or error.");    
     throw new HttpResponseException(message); 

     return " "; 
     // return "This command is null or error"; 
    } 
    else // si hay comandos cargo la fecha de envio y envio el string de comandos 
    { 
     _comand.Fecha_envio = DateTime.Now; 
     _context.SaveChanges(); 
     return "\nCOMMAND\n" + _comand.Str_Comando + "\n"; //envio la palabra COMMAND y despues el string con los comandos 
    } 
} 

工作正常,但希望看到不使用異常,探測器以各種方式的可能性,並不能。

從已經非常感謝你

回答

0

不知道爲什麼你還沒有收到這個問題的答案,但你嘗試下面的方法呢?

public class MyController 
{ 
    [Route("the/url/you/want"), HttpGet] 
    public IHttpActionResult Get() 
    { 
     return Ok("This is a string."); 
    } 
} 
相關問題