【发布时间】: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