webapi 通过dynamic 接收可变参数

时间:2023-03-09 17:31:36
webapi 通过dynamic 接收可变参数
 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": "", "JsonObj": { "Key1": "", "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",