我正在与Asp.Net中的"新"WebApi挣扎......
我只想发布一些Json,但它没有反序列化我的数据......我做错了什么?!
控制器类
public class UtilityController : ApiController
{
[HttpPost]
public string Bla(Bla bla)
{
return "bla";
}
}
Run Code Online (Sandbox Code Playgroud)
Bla类:
public class Bla
{
public string Een { get; set; }
public string Twee { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Api配置:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{Action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
发布数据:
var bla = $.parseJSON('{"Een":"UNO","Twee":"DUE"}');
$.ajax({
type: "POST",
url: "/api/utility/Bla",
data: {Bla : bla},
dataType: "json"
}).done(function( msg ) {
alert( "Data Saved: " + msg ); … 如果以@符号开头,如何将Json属性反序列化为动态对象.
{
"@size": "13",
"text": "some text",
"Id": 483606
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样得到id和text属性.
dynamic json = JObject.Parse(txt);
string x = json.text;
Run Code Online (Sandbox Code Playgroud)