How to pass complex object as $http params and receive as a view model in API controller action

I have an API endpoint. The controller action looks like this -

public IHttpActionResult Post(string entityType, SomeModel someModel)

Here SomeModel is a view model, which looks like this -

public class SomeModel
  {
    public long? Id { get; set; }
    public long EntityId { get; set; }
    public string Text { get; set; }
  }

I am calling this API endpoint from angularjs end -

return $http({
            method: 'POST',
            url: ENV.apiEndpoint + 'tags',
            params:{
              // here I want to pass parameters which will automatically map into API's two parameter(one string and the other view model)
            }
        });

I don't want to alter my API endpoint. My question is how I can pass parameter from angular end, which will automatically map into the argument of API method.

I have tried a number of things -

var someModel = {id: 1, text:"something", entityId: 12}
return $http({
            method: 'POST',
            url: ENV.apiEndpoint + 'tags',
            params:{
              entityType: entityType,
              someModel: JSON.stringify(someModel)
            }
        });

Or,

var someModel = {id: 1, text:"something"}
return $http({
            method: 'POST',
            url: ENV.apiEndpoint + 'tags',
            params:{
              entityType: entityType,
              id: 1,
              text: "something",
              entityId: 12
            }
        });

None of these seems to work. Meaning the params is not correctly getting mapped into the argument of API method. What is the ideal way to pass params in this type of scenario?

I don't want to change my API method's argument. I know I can use [FromBody] or [FromURI]. But I don't want to do those.

#c-sharp #angular.js #asp.net

4 Likes29.60 GEEK