[英]HttpClient PostAsync returns error 404 Not found in .net core 2.0
I am trying to call the webapi using httpclient. 我正在尝试使用httpclient调用webapi。 but it's not recognizing method and always throw 404 error.
但它不是识别方法,总是抛出404错误。
My api Action method is like this. 我的api Action方法是这样的。
[Route("AddEmployee")]
[HttpPost]
public async Task<IActionResult> PostTEmployee([FromBody]Employee emp)
{ // doing something here}
And the calling method is like below 调用方法如下
using (var httpClient = new HttpClient())
{i
String BaseUrl = "http://ip:port/";
httpClient.BaseAddress = new Uri(BaseUrl);
AuthenticationHeaderValue parsedAuthToken = null;
bool authParsed = AuthenticationHeaderValue.TryParse(authToken, out parsedAuthToken);
if (authParsed)
{
httpClient.DefaultRequestHeaders.Authorization = parsedAuthToken;
}
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string serialisedData = JsonConvert.SerializeObject(emp);
HttpContent content = new StringContent(serialisedData, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await httpClient.PostAsync("AddEmployee", content);
if (response.IsSuccessStatusCode)
{
}
}
I am stuck with this issue from 2 days. 我从2天开始就遇到这个问题。 Not sure what is the mistake?
不知道这是什么错误? please help me.
请帮我。
Totally a wild guess but is your endpoint /api/AddEmployee
not /AddEmployee
? 完全是一个疯狂的猜测,但是您的端点
/api/AddEmployee
不是/AddEmployee
吗?
try changing 尝试改变
response = await httpClient.PostAsync("AddEmployee", content);
To 至
response = await httpClient.PostAsync("api/AddEmployee", content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.