【问题标题】:How to send a JSON object using GET method如何使用 GET 方法发送 JSON 对象
【发布时间】:2017-08-16 16:15:12
【问题描述】:

我使用 WebApi2 的控制器方法

    [HttpGet]
    public IEnumerable<Products> GetProducts(ProductSearchCriteria searchCriteria)
    {
        //searchCriteria is always null here!!! Why?
        return db.Instance.LoadProducts(searchCriteria);
    }

我的搜索条件类

public class ProductSearchCriteria
{
    private int id;
    private string name;
    private DateTime createdOn;

    [JsonProperty]
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    [JsonProperty]
    public DateTime CreatedOn
    {
        get { return this.createdOn; }
        set { this.createdOn = value; }
    }

    [JsonProperty]
    public int ID
    {
        get { return this.id; }
        set { this.id = value; }
    }
}

我在html页面中的脚本

<script>
    $("#btnTest").on("click", function () {
        var searchCriteria = {};
        searchCriteria.ID = 0;
        searchCriteria.Name = "";
        //searchCriteria.CreatedOn = "";
        var url = "http://localhost:8080/api/products"
        $.getJSON(url, searchCriteria).done(processResponse);
    });

    function processResponse(response){
    }
</script>

我到达了我的控制器方法(调试模式),但 ProductSearchCriteria searchCriteria 参数始终为空。如何使用 JQuery 和 WebApi2 的 get 方法发送我的 JSON 对象?

【问题讨论】:

  • GET 请求不应该有正文,因为它将被服务器忽略,因此您必须将查询字符串上的数据作为纯参数传递,并使用 [FromUri] 或更好的方法标记处理程序, 使用 POST 方法。

标签: c# jquery json asp.net-web-api getmethod


【解决方案1】:

您正在使用 $.getJSON(url, searchCriteria)getJSON sends the searchCriteria as a url-encoded query string 将查询发送到服务器,因为您的 searchCriteria 将符合 plain object 的定义

在服务器端,.NET Web API 的默认参数绑定将在 URL 中查找“简单”数据类型(例如 int、double、string),否则将回退到正文内容。

要获取 Web API 模型绑定以从 url 中提取复杂类型,例如您的 ProductSearchCriteria 类,您需要在参数前面添加 [FromUri] 属性,如下所示:

[HttpGet]
public IEnumerable<Products> GetProducts([FromUri] ProductSearchCriteria searchCriteria) {}

更多详情请看这里Parameter Binding in ASP.NET Web API

我认为值得尝试保留 GET 语义而不是像一些人建议的那样切换到 POST,因为您的代码实际上正在执行看起来像 read 操作,只要您不修改数据或状态... GET 似乎适用。

【讨论】:

    【解决方案2】:

    您可以尝试使用 [FromUri] 来装饰您的参数。

    [HttpGet]
    public IEnumerable<Products> GetProducts([FromUri] ProductSearchCriteria searchCriteria)
    {
        //searchCriteria is always null here!!! Why?
        return db.Instance.LoadProducts(searchCriteria);
    }
    

    另一种选择是对 JSON 对象进行字符串化并在服务器端代码中对其进行压缩。您可以使用 JSON.NET 等转换器来执行此操作,也可以使用自定义类型转换器、模型绑定器或值提供程序。更多信息可以在here找到。

    【讨论】:

      【解决方案3】:

      改用帖子:

      $("#btnTest").on("click", function () {
          var searchCriteria = {};
          searchCriteria.ID = 0;
          searchCriteria.Name = "";
          //searchCriteria.CreatedOn = "";
          var url = "http://localhost:8080/api/products"
          $.post(url, data, processResponse, 'json');
      
      });
      

      并将方法属性更改为:

      [HttpPost]
      public IEnumerable<Products> GetProducts(ProductSearchCriteria searchCriteria)
      

      【讨论】:

        【解决方案4】:

        试试这个代码

        [HttpGet]
        public IEnumerable<Products> GetProducts([FromUri]ProductSearchCriteria searchCriteria)
        {
            //searchCriteria is always null here!!! Why?
            return db.Instance.LoadProducts(searchCriteria);
        }
        

        【讨论】:

          猜你喜欢
          • 2012-06-12
          • 2014-03-17
          • 2015-10-21
          • 1970-01-01
          • 1970-01-01
          • 2011-11-11
          • 2019-06-24
          • 1970-01-01
          • 2015-11-11
          相关资源
          最近更新 更多