I have the following post method in my WEB API controller:
我的WEB API控制器中有以下post方法:
public async Task<HttpResponseMessage> SendPost(Application application)
I call it through javascript using angular.js $http.post
and pass through the application parameter as JSON:
我使用angular.js $ http.post通过javascript调用它,并将应用程序参数作为JSON传递:
$http.post("/api/AController/SendPost", JSON.stringify(application)).
success(function (data, status, headers, config) {
}
This works.
Now I want to pass through a second parameter as a simple string (I can't modify the existing application JSON object).I tried a few different ways suggested on the web but none of them seem to work. I need to be able to do soemthing like this:
现在我想将第二个参数作为一个简单的字符串传递(我无法修改现有的应用程序JSON对象)。我尝试了几种不同的方式在网上建议,但它们似乎都没有工作。我需要能够像这样做:
Controller:
public async Task<HttpResponseMessage> SendPost(RentalApplication application,string test)
Javascript:
$http.post("/api/TessIntegration/SendPost", {application:JSON.stringify(application),test:"Some value"}).
success(function (data, status, headers, config) {
}
2 个解决方案
#1
4
You cannot get multiple objects from the body in WebAPI.
您无法在WebAPI中从正文中获取多个对象。
If you were passing two complex objects, the solution would be to wrap them into another complex object.
如果您传递两个复杂对象,解决方案是将它们包装到另一个复杂对象中。
public async Task<HttpResponseMessage> SendPost(SuperComplex request)
public class SuperComplex {
public Application Application { get; set; }
public AnotherObject Object { get; set; }
}
$http.post("/api/AController/SendPost", { application: application, Object: {} });
Now if the 2nd parameters is a simple object (such as a string) you can just pass it by queryString instead.
现在,如果第二个参数是一个简单的对象(如字符串),您可以通过queryString来传递它。
$http.post("/api/AController/SendPost?test=some+value", application );
Also, you don't have to stringify, Angular does it for you.
此外,您不必进行stringify,Angular会为您完成。
#2
2
Found a solution using Newtonsoft.Json.Linq.JObject:
使用Newtonsoft.Json.Linq.JObject找到解决方案:
Controller:
public async Task<HttpResponseMessage> SendPost(JObject data)
{
RentalApplication application = data["application"].ToObject<RentalApplication>();
string test = data["test"].ToObject<string>();
}
Javascript:
var data = {
application : application,
test : "sample value"
};
$http.post("/api/TessIntegration/SendPost",data).
success(function (data, status, headers, config) {
}
#1
4
You cannot get multiple objects from the body in WebAPI.
您无法在WebAPI中从正文中获取多个对象。
If you were passing two complex objects, the solution would be to wrap them into another complex object.
如果您传递两个复杂对象,解决方案是将它们包装到另一个复杂对象中。
public async Task<HttpResponseMessage> SendPost(SuperComplex request)
public class SuperComplex {
public Application Application { get; set; }
public AnotherObject Object { get; set; }
}
$http.post("/api/AController/SendPost", { application: application, Object: {} });
Now if the 2nd parameters is a simple object (such as a string) you can just pass it by queryString instead.
现在,如果第二个参数是一个简单的对象(如字符串),您可以通过queryString来传递它。
$http.post("/api/AController/SendPost?test=some+value", application );
Also, you don't have to stringify, Angular does it for you.
此外,您不必进行stringify,Angular会为您完成。
#2
2
Found a solution using Newtonsoft.Json.Linq.JObject:
使用Newtonsoft.Json.Linq.JObject找到解决方案:
Controller:
public async Task<HttpResponseMessage> SendPost(JObject data)
{
RentalApplication application = data["application"].ToObject<RentalApplication>();
string test = data["test"].ToObject<string>();
}
Javascript:
var data = {
application : application,
test : "sample value"
};
$http.post("/api/TessIntegration/SendPost",data).
success(function (data, status, headers, config) {
}