POST multiple objects from Angular controller to Web API 2

11,065

OK managed to get it working by following this article.

Then I was able to use simple and complex types in my api method signatures :

public JsonResult MyAction(StonglyTypedObject myObj, string description)

and I am passing in the values in the data parameter :

 $http({
            method: 'POST',
            url: targetUri,
            data: {
                myObj: myObj,
                description: "desc here"
            }
        }).success(...

This was the cleanest solution I could find, hope it helps you too.

Share:
11,065
Mike
Author by

Mike

Updated on August 06, 2022

Comments

  • Mike
    Mike almost 2 years

    I am able to send a raw json object from my angular controller which is deserialized to a known type at my web api method. This is great but I now need to be able to send other parameters in the same request, these could be json objects or simple types like string or int.

    I've seen articles such as this which describe exactly my problem but they are sending their request from codebehind rather than client side.

    I tried to construct a json array and send this in but I get the following message : 'Cannot create an abstract class'.

    Controller Code (adapted)

    var request = {
                params:[]
            };
    
            request.params.push(jsonObjectA);
            request.params.push({"someString" : "ABCDEF"});
    
            $http({
                method: 'POST',
                url: targetUri,
                data: request
            })
    

    Web API Method Signature

     public JsonResult TestMethod(JArray request)
    

    Does this approach sound sensible? I really want to avoid having to create dto objects for every request if I can.

  • Mike
    Mike about 9 years
    thanks but I also know how to find the angular docs : docs.angularjs.org/api/ng/service/$http also, please read the question more carefully in future, I was asking how I can add 2 parameters to the request rather than one as shown in the code snippet you posted.