I am trying to call a .net Web Api method via jquery's $.ajax. When I pass in
我正在尝试通过jquery的$.ajax调用。net Web Api方法。当我通过
var process = "first process", var nameArray = ["first", "second"], var valueArray = ["val1", "val2"]
var process =“first process”,var nameArray = ["first", "second"], var valueArray = ["val1", "val2"]
and then:
然后:
$.ajax({
url: jsonFeed,
data: {process: process, nameArray: nameArray, valueArray: valueArray},
etc...
I have a ASP.NET Web Api method:
我有一个ASP。NET Web Api方法:
public string GetResponseToken(string process, string[] nameArray, string[] valueArray)
When I run everything, I receive an error message:
当我运行一切时,我收到一条错误消息:
"Can't bind multiple parameters ('nameArray' and 'valueArray') to the request's content."
不能将多个参数('nameArray'和'valueArray')绑定到请求的内容。
Does anyone know why this is, or how I can fix it to accept my arrays?
有人知道这是为什么吗?或者我如何修复它以接受数组?
1 个解决方案
#1
11
The Web.API parameter/model binder work differently than the MVC one. You need to tell it that you wan't to bind all of your arguments from the query string with the [FromUri]
attribute:
网络。API参数/模型绑定器的工作方式与MVC不同。您需要告诉它,您不想将查询字符串中的所有参数与[FromUri]属性绑定:
public string GetResponseToken(
[FromUri]string process,
[FromUri]string[] nameArray,
[FromUri]string[] valueArray)
{
//...
}
In the long run (e.g the above mentioned approach won't work if you your request type is a POST) you should consider to use a parameter object instead of having multiple arguments.
从长远来看(e。如果您的请求类型是POST,那么上面提到的方法将不起作用)您应该考虑使用参数对象,而不是使用多个参数。
public string GetResponseToken([FromUri]ResponseTokenRequest request)
{
//...
}
public class ResponseTokenRequest
{
public string Process { get; set; }
public string[] NameArray { get; set; }
public string[] ValueArray { get; set; }
}
You can learn about the Wep.API parameter binding in the following articles:
你可以了解Wep。API参数绑定在以下文章中:
- How WebAPI does Parameter Binding
- 参数绑定是如何实现的
- Passing multiple POST parameters to Web API Controller Methods
- 将多个POST参数传递给Web API控制器方法
#1
11
The Web.API parameter/model binder work differently than the MVC one. You need to tell it that you wan't to bind all of your arguments from the query string with the [FromUri]
attribute:
网络。API参数/模型绑定器的工作方式与MVC不同。您需要告诉它,您不想将查询字符串中的所有参数与[FromUri]属性绑定:
public string GetResponseToken(
[FromUri]string process,
[FromUri]string[] nameArray,
[FromUri]string[] valueArray)
{
//...
}
In the long run (e.g the above mentioned approach won't work if you your request type is a POST) you should consider to use a parameter object instead of having multiple arguments.
从长远来看(e。如果您的请求类型是POST,那么上面提到的方法将不起作用)您应该考虑使用参数对象,而不是使用多个参数。
public string GetResponseToken([FromUri]ResponseTokenRequest request)
{
//...
}
public class ResponseTokenRequest
{
public string Process { get; set; }
public string[] NameArray { get; set; }
public string[] ValueArray { get; set; }
}
You can learn about the Wep.API parameter binding in the following articles:
你可以了解Wep。API参数绑定在以下文章中:
- How WebAPI does Parameter Binding
- 参数绑定是如何实现的
- Passing multiple POST parameters to Web API Controller Methods
- 将多个POST参数传递给Web API控制器方法