我找到了很多关于这个主题的信息,但是这些网站和文章都不能解决我的问题。我有一个非常简单的方法:
[HttpPost, Route("Diagnosis/{lkNo}/Tree/{nodeID:int}/Answer")]
public List<TreeNode> AnswerTreeNode(string lkNo, int nodeID,
[FromBody] dynamic data) {
// So some stuff
}
当我调用该方法时,它会填充前两个参数,但数据始终为空。这是服务器收到的我的测试请求:
POST /Diagnosis/LK-28084453/Tree/0/Answer HTTP/1.1
Cache-Control: no-cache
Connection: keep-alive
Accept: */*
Accept-Encoding: gzip, deflate
Cookie: ASP.NET_SessionId=*****; __RequestVerificationToken=*****
Host: localhost:51124
User-Agent: PostmanRuntime/7.6.0
Postman-Token: *****
Content-Length: 5
Content-Type: application/x-www-form-urlencoded
=Test
将参数作为 json 发送会导致相同的结果:
...
Content-Length: 25
Content-Type: application/json
{ "value": "some value" }
无论我尝试什么,数据始终为空。这是我的路线配置:
// WebAPI
GlobalConfiguration.Configure(config => {
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DiagnosisApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Default return JSON
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
config.MessageHandlers.Add(new MyHandler());
});
public class MyHandler : System.Net.Http.DelegatingHandler {
protected override async System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(
System.Net.Http.HttpRequestMessage request,
System.Threading.CancellationToken token) {
System.Net.Http.HttpMessageContent requestContent = new System.Net.Http.HttpMessageContent(request);
string requestMessage = requestContent.ReadAsStringAsync().Result; // This one contains the raw requests as posted abve
return await base.SendAsync(request, token);
}
}
你有什么想法,这里有什么问题吗?