使用angular post将两个参数传递给WEB API调用

我的WEB API控制器中有以下post方法:

public async Task SendPost(Application application) 

我使用angular.js $http.post通过javascript调用它,并将应用程序参数作为JSON传递:

 $http.post("/api/AController/SendPost", JSON.stringify(application)). success(function (data, status, headers, config) { } 

这很有效。

现在我想将第二个参数作为一个简单的字符串传递(我无法修改现有的应用程序JSON对象)。我尝试了几种不同的方式在网上建议,但它们似乎都没有工作。 我需要能够像这样做:

控制器:

 public async Task SendPost(RentalApplication application,string test) 

使用Javascript:

  $http.post("/api/TessIntegration/SendPost", {application:JSON.stringify(application),test:"Some value"}). success(function (data, status, headers, config) { } 

您无法在WebAPI中从正文中获取多个对象。

如果您传递两个复杂对象,解决方案是将它们包装到另一个复杂对象中。

 public async Task SendPost(SuperComplex request) public class SuperComplex { public Application Application { get; set; } public AnotherObject Object { get; set; } } $http.post("/api/AController/SendPost", { application: application, Object: {} }); 

现在,如果第二个参数是一个简单的对象(如字符串),您可以通过queryString来传递它。

 $http.post("/api/AController/SendPost?test=some+value", application ); 

此外,您不必进行stringify,Angular会为您完成。

使用Newtonsoft.Json.Linq.JObject找到解决方案:

控制器:

 public async Task SendPost(JObject data) { RentalApplication application = data["application"].ToObject(); string test = data["test"].ToObject(); } 

使用Javascript:

  var data = { application : application, test : "sample value" }; $http.post("/api/TessIntegration/SendPost",data). success(function (data, status, headers, config) { 

}