如何在API控制器动作中把复杂的对象作为$http参数传递并作为视图模型接收?[英] How to pass complex object as $http params and receive as a view model in API controller action

本文是小编为大家收集整理的关于如何在API控制器动作中把复杂的对象作为$http参数传递并作为视图模型接收?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个API端点.控制器动作看起来像这样 -

public IHttpActionResult Post(string entityType, SomeModel someModel)

在这里SomeModel是一个视图模型,看起来像这样 -

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

我从angularjs end-

称此API端点
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)
            }
        });

我不想更改我的API端点.我的问题是如何从角端传递参数,该参数将自动映射到API方法的参数中.

我尝试了许多事情 -

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

或,

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

这些似乎都没有用.意味着这些参数无法正确地映射到API方法的参数中.在这种情况下传递参数的理想方法是什么?

我不想更改我的API方法的参数.我知道我可以使用[FromBody] or [FromURI].但是我不想这样做.

推荐答案

您应该能够做

    $http({
        method: 'POST',
        url: ENV.apiEndpoint + 'tags',
        params:{
          entityType: entityType
        },
        data: someModel
    });

默认情况下,来自http message body( data )的发布请求中的deleializes(参考)对象参数( data )以及来自URL参数( params ).

本文地址:https://www.itbaoku.cn/post/1937859.html

问题描述

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.

推荐答案

You should be able to do

    $http({
        method: 'POST',
        url: ENV.apiEndpoint + 'tags',
        params:{
          entityType: entityType
        },
        data: someModel
    });

By default web api (and asp.net mvc) in a POST request deserializes (reference) object arguments from http message body (data) and value types and string arguments from URL parameters (params).