C# 状态代码:404,原因短语:“未找到”,版本:1.1,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16130908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:06:02  来源:igfitidea点击:

StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1,

c#asp.net-web-api

提问by Peyman Tahmasebi

I use Web api selef host:

我使用 Web api 自主机:

public class TestController : ApiController
{
    [HttpPost]
    public void Testp([FromBody]string title)
    {
        Console.WriteLine("Post");
    }
}

this is simple controller and this is my client:

这是一个简单的控制器,这是我的客户:

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;

                }

why my result is:

为什么我的结果是:

{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
}}

I thnik my modelbinder has some error

我认为我的模型绑定器有一些错误

采纳答案by m.edmondson

I'd imagine you're debugging using the visual studio web server (down by the clock). The port for this can change at any time, so I'm guessing it is no longer '1010' as specified in your URL:

我想您正在使用 Visual Studio Web 服务器进行调试(按时钟计时)。此端口可以随时更改,因此我猜它不再是您的 URL 中指定的“1010”:

"http://localhost:1010"

"http://localhost:1010"

Perhaps you should look hereto get the current URL automatically.

也许您应该查看此处以自动获取当前 URL。

回答by Filip W

  1. If you are using the default routing, it's RESTful (by HTTP verb, rather than RPC, by method name) so you shouldn't be posting to http://localhost:1010/Test/Testpbut http://localhost:1010/Test

  2. Your action signature takes a string but you are POSTing an anonymous object instead. Your POST should look like this (notice that we're sending just the string):

    var post = client.PostAsync("Test", englishTitle, new JsonMediaTypeFormatter());

  1. 如果您使用的是默认路由,则它是 RESTful(通过 HTTP 动词,而不是 RPC,通过方法名称),因此您不应该发布到http://localhost:1010/Test/Testphttp://localhost:1010/Test

  2. 您的操作签名采用一个字符串,但您正在发布一个匿名对象。您的 POST 应如下所示(请注意,我们仅发送字符串):

    var post = client.PostAsync("Test", englishTitle, new JsonMediaTypeFormatter());

回答by eaglei22

I figured out my issue was if I didn't end the base URI with "/" and tried to pre-pend it to client.PostAsync(.. it wouldn't work. But if I appended to the base uri it would like so,

我发现我的问题是如果我没有用“/”结束基本 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;

worked, while:

工作,同时:

 using (HttpClient client = new HttpClient(handler) { BaseAddress = new Uri("https://test.test.com/somepath") })
 var response = client.PostAsync("/Broadcast/Order", content).Result;

Does not. Not sure why, but glad I figure it out pretty quick!

才不是。不知道为什么,但很高兴我很快就弄清楚了!

回答by Farookh Mansuri

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"];

I am facing same problem. But Now resolved. You can use this code. it will really help you.

我面临同样的问题。但是现在解决了。您可以使用此代码。它真的会帮助你。