【问题标题】:ASP.NET MVC Project: RESTful API method with complex input parametersASP.NET MVC 项目:具有复杂输入参数的 RESTful API 方法
【发布时间】:2014-02-28 07:23:11
【问题描述】:

我是设计 API 的新手,我在 ASP.NET MVC 中遇到过情况。

在我的域系统中,我有不同的概念,例如Invoice。我想创建一个 REST API,它可以:

  • 创建
  • 更新
  • 删除
  • 选择(基于不同的元素)

但是,例如,当创建一个新对象时,我需要一大组参数(参见下面的示例视图模型)。

如果我期望这样的路径:

POST - /api/invoice/create

我将如何四处走动并接受表单数据?

我最好的猜测是创建一个 APIController,然后接受 InvoiceViewModel 作为唯一参数。由于它是一个 API 控制器,我假设它默认接受 JSON。

那么我有以下问题:

  • 在 jQuery 中,我将如何构建一个 JSON 对象来“满足”这个InvoiceViewModel
  • 这是处理更复杂产品的最佳方式吗?

InvoiceViewModel

 public class InvoiceViewModel
    {
        public int Id { get; set; }

        public string Comment { get; set; }

        public InvoiceAddressViewModel CompanyInfo { get; set; }
        public InvoiceAddressViewModel ReceiverInfo { get; set; }

        public DateTime DateCreated { get; set; }

        public List<InvoiceLineViewModel> Lines { get; set; }

        public decimal Total { get; set; }
        public decimal VatTotal { get; set; }
        public decimal VatPercentage { get; set; }
        public decimal TotalExVat { get; set; }

        public InvoiceViewModel()
        {
            this.Lines = new List<InvoiceLineViewModel>();
        }
    }

    public class InvoiceAddressViewModel
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Company { get; set; }
        public string VatNumber { get; set; }
        public string Country { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
    }

    public class InvoiceLineViewModel
    {
        public string Title { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
    }

【问题讨论】:

    标签: c# asp.net-mvc api asp.net-mvc-4 rest


    【解决方案1】:

    Asp.net-mvc 3 框架有一个默认的 JsonValueProviderFactory,只要您发布到 Action 的 JSON 数据与它应该正确绑定数据的模型匹配。

    所以是这样的:

      var requestData = {
        Id: "1",
        Comment: "The quick brown fox",
        CompanyInfo: {
            Name: "etc."
        }
      };
    
      $.ajax({
         url: '/api/invoice/create',
         type: 'POST',
         data: JSON.stringify(requestData),
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         error: function (xhr) {
            alert('Error: ' + xhr.statusText);
         },
         success: function (result) {
            alert('success');
         }
      });
    

    应该可以正常工作,所有数据都应该正确绑定,只需将 JSON 属性名称与模型名称匹配即可。(它们必须匹配)

    让您的模型匹配的最佳方法是简单地将您的模型在您的视图中序列化为 JSON,然后您就知道它是正确的,然后根据您的内心进行操作。

    var data = set(@Html.Raw(new JavaScriptSerializer().Serialize(Model)));

    至于复杂性,你可能会(非常不可能)遇到一些情况,因为 .net 的默认 JsonValueProviderFactory 不够好,但如果你这样做了,我会感到惊讶。

    顺便说一句,整个用例与http://knockoutjs.com/ 完美配合。

    玩得开心。

    【讨论】:

    • 要添加到上述内容,您还可以使用 Ajax.BeginForm 并在 AjaxOptions 的 Url 参数中指定您的 web api url。请看一下这个stackoverflow帖子的答案stackoverflow.com/questions/16510873/…
    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 2016-09-05
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多