public class JsonParamModel { /// <summary> /// json key /// </summary> public string JsonKey { get; set; } /// <summary> /// json object /// </summary> public dynamic JsonObj { get; set; } }
[HttpPost] public Result WebapiDynamic(JsonParamModel json)
$.ajax({ type: "POST", contentType: "application/json", dataType: "json", url: $("#url").val(), data: JSON.stringify({"JsonKey": "13", "JsonObj": { "Key1": "11", "Key2": "-10" } }), success: function (results) { if (typeof results == "object") { JSON.stringify(results); } else { var resultObj = JSON.parse(results); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText); } });
总结:在实体或者直接在参数中采用dynamic可以动态的接收参数
但是ajax在调用的时候稍微有一点不同
data: JSON.stringify({"JsonKey": "13", "JsonObj": { "Key1": "11", "Key2": "-10" } }),
contentType: "application/json",