Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 890
Peyman Tahmasebi Avatar asked Feb 16 '23 18:02

Peyman Tahmasebi


2 Answers

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.

like image 55
m.edmondson Avatar answered Feb 27 '23 11:02

m.edmondson


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!

like image 43
eaglei22 Avatar answered Feb 27 '23 12:02

eaglei22