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