我使用Web api selef host:
public class TestController : ApiController
{
[HttpPost]
public void Testp([FromBody]string title)
{
Console.WriteLine("Post");
}
}这是一个简单的控制器,这是我的客户端:
client.BaseAddress = new Uri("http://localhost:1010");
const string englishTitle = "TesteDelete";
var post = client.PostAsync("Test/Testp", new
{
title = englishTitle
}, new JsonMediaTypeFormatter());
var result = post.Result;
if (result.IsSuccessStatusCode)
{
}
else
{
string content = result.Content.ReadAsStringAsync().Result;
}为什么我的结果是:
{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Sun, 21 Apr 2013 12:00:03 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 165
Content-Type: application/json; charset=utf-8
}}我想我的模型绑定器有一些错误。
发布于 2013-04-21 20:11:01
我可以想象您正在使用visual studio web服务器(按时钟关闭)进行调试。这个端口可以随时更改,所以我猜它不再是URL中指定的'1010‘了:
"http://localhost:1010"
也许你应该使用look here来自动获取当前的网址。
发布于 2013-04-22 11:04:09
http://localhost:1010/Test/Testp,但是var post = client.PostAsync("Test", englishTitle, new JsonMediaTypeFormatter());
发布于 2017-05-26 06:49:44
我发现我的问题是,如果我没有以"/“结束基本URI,并尝试将其前置为client.PostAsync(..那是行不通的。但是如果我附加到基本uri,它会这样,
using (HttpClient client = new HttpClient(handler) { BaseAddress = new Uri("https://test.test.com/somepath/") })
var response = client.PostAsync("Broadcast/Order", content).Result;工作,同时:
using (HttpClient client = new HttpClient(handler) { BaseAddress = new Uri("https://test.test.com/somepath") })
var response = client.PostAsync("/Broadcast/Order", content).Result;不会。不知道为什么,但很高兴我很快就弄明白了!
https://stackoverflow.com/questions/16130908
复制相似问题