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
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With