【问题标题】:Why I get null when I make post JSON to web API method?为什么我在将 JSON 发布到 Web API 方法时得到 null?
【发布时间】:2015-11-17 12:01:06
【问题描述】:

我创建 JSON 并将其从 cilent 发送到 web api 方法。但我在端点函数中得到 NULL。

我有这个功能:

function genarateDirectives(workPlanServise) {
    var dataObj = {
        name: 'name',
        employees: 'employee',
        headoffice: 'master'
    };

    return workPlanServise.generateFilter(dataObj)
        .then(function (result) {
            return result.data;
        });
}

这是我使用的服务:

(function () {
    "use strict";

    angular.module("workPlan").factory("workPlanServise", ["$http", "config", workPlanServise]);

    function workPlanServise($http, config) {
        var serviceUrl = config.baseUrl + "api/workPlan/";
        var service = {
            getAll: getAll,
            getSubGridContent: getSubGridContent,
            generateFilter:generateFilter
        };
        return service;

        function getAll() {
            return $http.get(serviceUrl);
        }

        function getSubGridContent(clientId) {
            return $http.get(serviceUrl + '?clientId=' + clientId);
        }

        function generateFilter(objData) {
            return $http.post(serviceUrl, objData );
        }
    }
})();

这里端点web api函数:

    [HttpPost]
    public async Task<IHttpActionResult> Post([FromBody]string objData) 
    {
        try
        {
            return null;
        }
        catch (Exception)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
        }
    }

知道为什么 objData 总是为空吗?

【问题讨论】:

    标签: angularjs asp.net-mvc asp.net-web-api asp.net-web-api2


    【解决方案1】:

    因为您将 JSON 对象绑定到无效的字符串。创建模型

    public class MyModel
    {
        public string Name { get; set; }
    
        public string Employees { get; set; }
    
        public string HeadOffice { get; set; }
    }
    

    并且在没有 [FromBody] 属性的动作中使用它,因为默认情况下所有引用类型都是从主体绑定的。

    public async Task<IHttpActionResult> Post(MyModel objData) 
    {
        // work with objData here
    }
    

    【讨论】:

      【解决方案2】:

      创建一个模型类,其字段与您的 Web api 中的 objData 中的字段匹配。

      web api 模型绑定器将为您填充它。不要忘记检查请求是否在标头中包含 contentType: "application/json"。 (一个标准的 $http 调用就会有这个)

      例如:

      public class SomeModel
      {
          public string Name { get; set; }
      
          public int Number { get; set; }
      
          public string Description { get; set; }
      }
      

      然后发布到:

      [HttpPost]
      public async Task<IHttpActionResult> Post(SomeModel objData) 
      { ....
      

      如果您确实需要将字符串发布到 Web api,则需要在请求的标头中传递 text/plain 而不是 application/json,并向 Web api 添加额外的 text/plain 格式化程序。 See here for more info

      【讨论】:

      • 不想创建DTO怎么办?没有DTO也能得到结果吗?
      • @Michael 你可以,但是你应该在来自 $http 的请求中添加 content-type: text/plain
      • 如果我使用 content-type: text/plain 是否必须使用 [FromBody] 属性?
      • 抱歉,我忘了说你应该有一个额外的格式化程序:myadventuresincoding.wordpress.com/2012/06/19/…,我现在看到我在我的一个项目中得到了它
      猜你喜欢
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2015-12-13
      • 2021-07-10
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多