Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
up vote 4 down vote accepted

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|improve this answer
// 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..

share|improve this answer
2  
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. – Mike Mar 3 '15 at 10:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.