2017-05-10 1 views
1

Mon problème est lorsque j'essaie d'appeler mon service de repos de l'API Web à partir d'une application mvc en utilisant HttpClient PostAsync(), mon API web ne renvoie jamais le message de réponse.HTTPClient PostAsync() avec async et await ne renvoie jamais le message

Voici le code de l'application mvc:

public async Task<string> sendToWebAPI(string _obj) 
    { 
     using (HttpClient client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri(ConfigurationManager.AppSettings["WebAPIRestService"]); 
      StringContent _jsonParameter = new StringContent(_obj, Encoding.UTF8, "application/json"); 

      HttpResponseMessage Res = await client.PostAsync("api/webAPIController/", _obj).ConfigureAwait(false);    
      var WebAPIResponse = await Res.Content.ReadAsStringAsync(); 

      return WebAPIResponse; 

     } 
    } 

MVC Code API Web:

[HttpPost] 
    public async Task<string> Dowork([FromBody] string _obj) 
    { 
     HttpResponseMessage result = Request.CreateResponse(_obj != "" ? HttpStatusCode.OK : HttpStatusCode.InternalServerError); 
     return result; 
    } 

Merci!

+0

Veuillez utiliser camelCase pour nommer vos variables et PascalCase pour nommer vos méthodes. –

Répondre

0

Votre code API ne renvoie actuellement pas de chaîne. Il récupère OK ou InternalServerError. Modifiez votre code comme suit.

bool isOK = string.isNullOrEmpty(_obj); 
result = isOK ? request.CreateResponse<string>(HttpStatusCode.OK, _obj) : 
request.CreateResponse<string>(HttpStatusCode.InternalServerError, "Invalid Data");