简体   繁体   中英

Pass two parameters to WEB API call using angular post

I have the following post method in my WEB API controller:

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:

$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:

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) {
}

You cannot get multiple objects from the body in 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.

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

Also, you don't have to stringify, Angular does it for you.

Found a solution using 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) {

}

Update to @Denys answer. Use of JObject is not necessary. Simply use JSON.stringify(data) in your javascript as:

JAVASCRIPT:

var data = {
        application : application,
        test : "sample value"
    };

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

C#

Change definition of controller endpoint as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM