C# 状态代码:404,原因短语:';未找到';,版本:1.1,
我使用Web api selef主机:C# 状态代码:404,原因短语:';未找到';,版本:1.1,,c#,asp.net-web-api,C#,Asp.net Web Api,我使用Web api selef主机: public class TestController : ApiController { [HttpPost] public void Testp([FromBody]string title) { Console.WriteLine("Post"); } } 这是一个简单的控制器 这是我的客户: client.BaseAddress = new Uri("http://localhost:1010");
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
}}
我发现我的modelbinder有一些错误我想您正在使用visual studio web服务器(按时钟顺序)进行调试。此端口可以随时更改,因此我猜它不再是URL中指定的“1010”:
”http://localhost:1010“
也许您应该自动获取当前URL
http://localhost:1010/Test/Testp
但是http://localhost:1010/Test
var post=client.PostAsync(“Test”,英语标题,新的JsonMediaTypeFormatter())代码>
我发现我的问题是,如果我没有用“/”结束基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;
不知道。不知道为什么,但很高兴我很快就明白了!var url=url;
var url = URL;
var data = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("client_id", FRAppConfigKeys.GetValue("ClientId")),
new KeyValuePair<string, string> ("client_secret", FRAppConfigKeys.GetValue("ClientSecret")),
new KeyValuePair<string, string>("grant_type", FRAppConfigKeys.GetValue("GrantType") ),
new KeyValuePair<string, string>("username", FRAppConfigKeys.GetValue("UserName")),
new KeyValuePair<string, string> ("password", FRAppConfigKeys.GetValue("Password"))
}
);
HttpResponseMessage response = client.PostAsync(url, data).Result;
var result = response.Content.ReadAsStringAsync().Result;
Dictionary<string, string> tokenDictionary =
JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
string token = tokenDictionary["access_token"];
var数据=新FormUrlEncodedContent(新[]{
新的KeyValuePair(“客户端id”,FRAppConfigKeys.GetValue(“客户端id”),
新的KeyValuePair(“客户端密码”,FRAppConfigKeys.GetValue(“客户端密码”),
新的KeyValuePair(“grant_type”,FRAppConfigKeys.GetValue(“GrantType”)),
新的KeyValuePair(“用户名”,FRAppConfigKeys.GetValue(“用户名”),
新的KeyValuePair(“密码”,FRAppConfigKeys.GetValue(“密码”))
}
);
HttpResponseMessage response=client.PostAsync(url、数据).Result;
var result=response.Content.ReadAsStringAsync().result;
字典标记字典=
JsonConvert.DeserializeObject(结果);
字符串令牌=令牌字典[“访问令牌”];
我面临同样的问题。但现在已解决。您可以使用此代码。它将真正帮助您。我注意到您没有以“/”结尾您的基本uri。我面临同样的问题。但现在已解决。您可以使用此代码。它将真正帮助您。