2

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
  • 369
  • 1
  • 6
  • 24

2 Answers2

7

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.

Mike
  • 369
  • 1
  • 6
  • 24
0
// Simple POST request example (passing data) :
$http.post('/someUrl', {msg:'hello word!'}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

In post method first parameter is url and second one is object, what ever u want u can pass in this object....

It may be help for u..

  • 2
    thanks but I also know how to find the angular docs : https://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. – Mike Mar 03 '15 at 10:54