Reputation: 2806
I'm working with an HttpSelfHostServer in .Net 4.5 and it seems to only be able to determine the controller and action when I send a request using QueryString. It doesn't work if I use "application/x-www-form-urlencoded".
Here's the HttpSelfHostServer code.
private static HttpSelfHostConfiguration _config;
private static HttpSelfHostServer _server;
public static readonly string SelfHostUrl = "http://localhost:8989";
internal static void Start()
{
_config = new HttpSelfHostConfiguration(SelfHostUrl);
_config.HostNameComparisonMode = HostNameComparisonMode.Exact;
_config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = RouteParameter.Optional },
constraints: null);
_server = new HttpSelfHostServer(_config);
_server.OpenAsync().Wait();
}
The controller code.
public class SettingsController : ApiController
{
[HttpPost]
public bool Test(bool work)
{
return work;
}
}
Here is the response I get when attempting to access it via REST Console using
Request URL: http://localhost:8989/api/Settings/Test
Request Method:POST
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:9
Content-Type:application/x-www-form-urlencoded
Host:localhost:8989
Origin:chrome-extension://cokgbflfommojglbmbpenpphppikmonn
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Form Dataview parsed
work=true
Response Headersview source
Content-Length:205
Content-Type:application/json; charset=utf-8
Date:Mon, 23 Dec 2013 22:41:10 GMT
Server:Microsoft-HTTPAPI/2.0
So, if I change my request to a post to the url below, it works.
http://localhost:8989/api/Settings/Test?work=true
Why isn't Content-Type:application/x-www-form-urlencoded working?
Upvotes: 0
Views: 1261
Reputation: 19311
Your action method parameter is bool
, which is a simple type. By default, ASP.NET Web API populates it from URI path or query string. That is the reason http://localhost:8989/api/Settings/Test?work=true
works.
When you send the same in the request body, it does not work because ASP.NET Web API binds body to a complex type (class) by default and hence the body will not be bound to your argument type bool
. To ask Web API to bind the simple type from body, change your action method like this.
public bool Test([FromBody]bool work)
Then, you will need to send only the value in the body, like this.
=true
.
Upvotes: 2
Reputation: 35477
The problem is not with the Content-Type. The rest console uses AJAX and CORS, see http://en.wikipedia.org/wiki/Cross-origin_resource_sharing. This is indicated by the Origin
http header in the request.
The server that contains the SettingsController
must support CORS. If it doesn't the AJAX request will always return a 404.
The easiest way for you to support CORS is to always return a Access-Control-Allow-Origin: *
in the request HTTP headers.
Upvotes: 0