[英]HttpSelfHostServer not recognizing “application/x-www-form-urlencoded” Requests
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. 我正在使用.Net 4.5中的HttpSelfHostServer,它似乎只能在我使用QueryString发送请求时确定控制器和操作。 It doesn't work if I use "application/x-www-form-urlencoded".
如果我使用“application / x-www-form-urlencoded”它不起作用。
Here's the HttpSelfHostServer code. 这是HttpSelfHostServer代码。
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 以下是尝试通过REST控制台访问时获得的响应
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. 所以,如果我将我的请求更改为帖子到下面的URL,它就可以了。
http://localhost:8989/api/Settings/Test?work=true
Why isn't Content-Type:application/x-www-form-urlencoded working? 为什么不是Content-Type:application / x-www-form-urlencoded工作?
Your action method parameter is bool
, which is a simple type. 你的action方法参数是
bool
,这是一个简单的类型。 By default, ASP.NET Web API populates it from URI path or query string. 默认情况下,ASP.NET Web API会从URI路径或查询字符串填充它。 That is the reason
http://localhost:8989/api/Settings/Test?work=true
works. 这就是
http://localhost:8989/api/Settings/Test?work=true
。
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
. 当您在请求正文中发送它时,它不起作用,因为默认情况下ASP.NET Web API将正文绑定到复杂类型(类),因此正文将不会绑定到您的参数类型
bool
。 To ask Web API to bind the simple type from body, change your action method like this. 要让Web API绑定body中的简单类型,请更改此操作方法。
public bool Test([FromBody]bool work)
Then, you will need to send only the value in the body, like this. 然后,您将需要仅发送正文中的值,如下所示。
=true
. =true
。
The problem is not with the Content-Type. 问题不在于Content-Type。 The rest console uses AJAX and CORS, see http://en.wikipedia.org/wiki/Cross-origin_resource_sharing .
其余控制台使用AJAX和CORS,请参阅http://en.wikipedia.org/wiki/Cross-origin_resource_sharing 。 This is indicated by the
Origin
http header in the request. 这由请求中的
Origin
http标头指示。
The server that contains the SettingsController
must support CORS. 包含
SettingsController
的服务器必须支持CORS。 If it doesn't the AJAX request will always return a 404. 如果不是,AJAX请求将始终返回404。
The easiest way for you to support CORS is to always return a Access-Control-Allow-Origin: *
in the request HTTP headers. 支持CORS的最简单方法是始终在请求HTTP头中返回
Access-Control-Allow-Origin: *
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.