Peyman Tahmasebi
Peyman Tahmasebi

Reputation: 193

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

I use Web api selef host:

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

Upvotes: 7

Views: 50555

Answers (4)

Farookh Mansuri
Farookh Mansuri

Reputation: 41

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.

Upvotes: 1

eaglei22
eaglei22

Reputation: 2830

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,

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!

Upvotes: 7

m.edmondson
m.edmondson

Reputation: 30922

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:

"http://localhost:1010"

Perhaps you should look here to get the current URL automatically.

Upvotes: 5

Filip W
Filip W

Reputation: 27187

  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/Testp but 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());

Upvotes: 1

Related Questions