编写web api项目和我的方法中的参数之一(这是一个json数组)将进入api null。 jQuery的我正在通话与这个样子的:web api方法调用中的参数为空
<script>
$(document).ready(function() {
$('#btnSubmit').click(function() {
var jsRequestAction = {
appId: 'appName',
custId: 'custId',
oprId: 'oprId',
businessProcess: 'Requisition',
action: 'Approve',
actionKeys: [
'blah blah 1',
'blah blah 2',
'blah blah 3'
]
};
$.ajax({
type: "POST",
content: "json",
url: "http://localhost/api/appName/custId/oprId",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ requestAction: jsRequestAction })
});
});
});
</script>
我的Web API方法是这样的:
public IList<ResponseAction> ActionCounter(string appName, string custCode, string custUserName, RequestAction requestAction)
{
IList<ResponseAction> actionResponseList = new List<ResponseAction>();
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
conn.Open();
try
{
foreach (string s in requestAction.actionKeys)
{
var command = new SqlCommand
{
CommandText = "Sql statement",
Connection = conn
};
command.ExecuteNonQuery();
var reply = new ResponseAction();
reply.responseActionKey = s;
reply.responseMessage = "Success";
actionResponseList.Add(reply);
}
return actionResponseList;
}
finally
{
conn.Close();
conn.Dispose();
}
}
RequestAction型号:
public class RequestAction
{
public string appId { get; set; }
public string custId { get; set; }
public string oprId { get; set; }
public string businessProcess { get; set; }
public string action { get; set; }
public string[] actionKeys { get; set; }
public string actionKey { get; set; }
}
当我调试,我通过该方法,当我到达foreach循环,我得到一个空对象引用。看看我的locals部分,我所有的requestAction属性都是null。在阅读了一些相关文章后,我试着用[FromBody]标签前缀对象来无效。任何帮助,将不胜感激。
从web api方法中除去了requestAction之外的所有参数,我仍然得到空值。我应该提到,我正在将此方法从我之前构建的WCF休息服务中移除,但我在web api中遇到了这个问题。 –