【发布时间】:2017-05-10 06:05:34
【问题描述】:
我的问题是当我尝试使用 HttpClient PostAsync() 从 mvc 应用程序调用我的 Web API REST 服务时,我的 Web API 永远不会返回响应消息。
这里是 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 Web API 代码:
[HttpPost]
public async Task<string> Dowork([FromBody] string _obj)
{
HttpResponseMessage result = Request.CreateResponse(_obj != "" ? HttpStatusCode.OK : HttpStatusCode.InternalServerError);
return result;
}
谢谢!
【问题讨论】:
-
请使用 camelCase 命名变量,使用 PascalCase 命名方法。
标签: c# asp.net asp.net-mvc asynchronous asp.net-web-api