简体   繁体   English

使用ASP.NET WEB API进行动态模型绑定

[英]Dynamic model binding with ASP.NET WEB API

According to Scott Hanselman, on his blog , I should be able to do dynamic model binding and return a dynamic. 根据Scott Hanselman的说法,在他的博客上 ,我应该能够进行动态模型绑定并返回动态。

I have a Web API controller that contains a single method: 我有一个包含单个方法的Web API控制器:

public dynamic Post(dynamic data)
{
     return data;
}

When I make the following call from Fiddler, I am getting a null returned. 当我从Fiddler进行以下调用时,我得到一个null返回。

POST http://localhost:57856/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:57856
Content-Type: "application/json"
Content-Length: 22

{"Name": "jlucpicard"}

What am I missing here? 我在这里错过了什么? Shouldn't it return JSON for data ? 它不应该为data返回JSON吗? This is a simpler follow-up to my original question ASP.NET WEB API not binding to dynamic object on POST . 这是一个更简单的后续我原来的问题ASP.NET WEB API没有绑定到POST上的动态对象

Your action is returning null because your "data" parameter is not being bound to the incoming json data. 您的操作返回null,因为您的“data”参数未绑定到传入的json数据。

Remove the quotes from "application/json" in your Content-Type header to bind to the data. 从Content-Type标头中的“application / json”中删除引号以绑定到数据。

Content-Type: application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM