ASP.NET MVC项目:复杂的输入参数的RESTful API方法 [英] ASP.NET MVC Project: RESTful API method with complex input parameters

查看:810
本文介绍了ASP.NET MVC项目:复杂的输入参数的RESTful API方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新设计的API,而我在ASP.NET MVC的情况。

在我的域名系统,我有不同的概念,如发票。我想创建一个REST API,它是可能的:


  • 创建

  • 更新

  • 删除

  • 选择(根据不同的元素)

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

如果我期望的路径像这样:

  POST  -  / API /发票/创建

我将如何去走一走,接受表单数据?

我最好的猜测是让一个APIController,然后接受 InvoiceViewModel 作为唯一的参数。由于它是一个API控制,我认为它在默认情况下接受JSON。

然后,我有以下问题(补):


  • 在jQuery的,我将如何构建一个JSON对象,以满足这个 InvoiceViewModel

  • 这是处理更复杂产品的最佳方法是什么?

InvoiceViewModel

 公共类InvoiceViewModel
    {
        公众诠释标识{搞定;组; }        公共字符串评论{搞定;组; }        公共InvoiceAddressViewModel公司信息{搞定;组; }
        公共InvoiceAddressViewModel ReceiverInfo {搞定;组; }        公众的DateTime dateCreated会获得{;组; }        公开名单< InvoiceLineViewModel>行{搞定;组; }        公共小数总{搞定;组; }
        公共小数VatTotal {搞定;组; }
        公共小数VatPercentage {搞定;组; }
        公共小数TotalExVat {搞定;组; }        公共InvoiceViewModel()
        {
            this.Lines =新的List< InvoiceLineViewModel>();
        }
    }    公共类InvoiceAddressViewModel
    {
        公共字符串名称{;组; }
        公共字符串地址{搞定;组; }
        公共字符串公司{搞定;组; }
        公共字符串VATNUMBER {搞定;组; }
        公共字符串国家{搞定;组; }
        公共字符串邮编code {搞定;组; }
        公共字符串城{搞定;组; }
    }    公共类InvoiceLineViewModel
    {
        公共字符串名称{搞定;组; }
        公众诠释数量{搞定;组; }
        公共十进制价格{搞定;组; }
    }


解决方案

有一个默认的JsonValueProviderFactory的Asp.net-MVC框架3,它应该将数据绑定模型只要发布到你的行动JSON数据相匹配正确的。

因此​​,像这样:

  VAR =的RequestData {
    编号:1,
    评论:敏捷的棕色狐狸
    公司信息:{
        名:等。
    }
  };  $阿贾克斯({
     网址:'/ API /发票/制造',
     输入:POST,
     数据:JSON.stringify(的RequestData)
     数据类型:JSON,
     的contentType:应用/ JSON的;字符集= UTF-8,
     错误:功能(XHR){
        警报('错误:'+ xhr.statusText);
     },
     成功:函数(结果){
        警报(成功);
     }
  });

应该工作得很好,所有的数据应正确绑定,只是型号名称相匹配的JSON属性名。(它们必须匹配)

最好的办法让你的模型匹配是简单序列模型在视图中JSON,那么你知道它是正确的,它操纵你的心内容。

VAR数据=集(@ Html.Raw(新的JavaScriptSerializer()序列化(型号)));

至于复杂,天空是你可能(可能性很小)碰到的一些情形for .NET的默认JsonValueProviderFactory不够好了限制,但如果你这样做我会感到惊讶。

正如一个侧面说明,这整个的用例 http://knockoutjs.com/ 奇妙的作品。

玩得开心。

I am new to designing API's, and I have a situation in ASP.NET MVC.

In my domain system, I have different concepts, such as an Invoice. I want to create a REST API, where it is possible to:

  • Create
  • Update
  • Delete
  • Select (based on different elements)

But, for instance, when creating a new object, I need a big set of parameters (see an example viewmodel below).

If I expect a path such as this:

POST - /api/invoice/create

How would I go around and accept form data?

My best guess is to make an APIController, and then accept the InvoiceViewModel as the only parameter. As it is an API Controller, I assume it accepts JSON by default.

Then I have the following question(s):

  • In jQuery, how would I build a JSON object to "satisfy" this InvoiceViewModel?
  • Is this the best way to handle more complex products?

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; }
    }

解决方案

There is a default JsonValueProviderFactory for the Asp.net-mvc 3 framework, as long as your JSON data posted to the Action matches the Model it should bind the data correctly.

So something like this:

  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');
     }
  });

Should work just fine, all the data should be bound correctly, just match the JSON property names with the Model names.(they must match)

The best way to get your model to match is to simply serialize your Model in your view to JSON, then you know it is correct, and manipulate it to your hearts content.

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

As for complexity, the sky is the limit you may (very unlikely) run into some scenarios where the default JsonValueProviderFactory for .net is not good enough, but if you do I would be suprised.

Just as a side note, this whole use-case works wonderfully with http://knockoutjs.com/.

Have fun.

这篇关于ASP.NET MVC项目:复杂的输入参数的RESTful API方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆