![](/img/trans.png)
[英]How do I format and post Json in the post request body (RestSharp)
[英]How do I write a POST request in JSON to post a new product?
我正在开发 WebAPI。 我已经使用实体框架通过我和我的朋友编写的 C# 代码制作了一个 SQL 数据库。 我们在 SQL 数据库中的产品表基于以下代码:
public class Product
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "Product Number")]
public int ProductID { get; set; }
[StringLength(50, MinimumLength = 3)]
public string ProductName { get; set; }
[Range(0, 99999)]
public int Price { get; set; }
public int MarketID { get; set; }
public Market Market { get; set; }
public ICollection<Subscription> Subscriptions { get; set; }
public ICollection<ProductAssignment> ProductAssignments { get; set; }
}
当我们运行我们的解决方案并打开 Postman 发出发布请求时,我们不确定应该如何编写 JSON。 到目前为止,我们已经写了:
{
"ProductID": 1234,
"ProductName": "Testproduct",
"Price": 10,
"MarketID": 4321,
"Market":
{
"MarketID": 1111,
"Name": "Market1",
"Budget": 1.5,
"StartDate": 2019-11-28,
"ProductGuideID": 123
}
"Subscriptions":
{
"SubscriptionId":1234,
"ProductId": 1234,
"CustomerID": 1234,
"CustomerLoyalty": "A"
}
"ProductAssignments"
{
"ProductGuideId": 1234,
"ProductID": 1234
}
}
问题是我们不知道如何为 Market、Subscriptions 和 ProductAssignments 编写 JSON。
我们应该用 JSON 写什么来发布新产品?
编辑:
市场类:
public class Market
{
public int MarketID { get; set; }
[StringLength(50, MinimumLength = 3)]
public string Name { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal Budget { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
public int? ProductGuideID { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public ProductGuide Administrator { get; set; }
public ICollection<Product> Products { get; set; }
}
订阅:
public enum CustomerLoyalty
{
A, B, C, D, F
}
public class Subscription
{
public int SubscriptionID { get; set; }
public int ProductID { get; set; }
public int CustomerID { get; set; }
[DisplayFormat(NullDisplayText = "Loyalty not set")]
public CustomerLoyalty? CustomerLoyalty { get; set; }
public Product Product { get; set; }
public Customer Customer { get; set; }
}
产品分配:
public class ProductAssignment
{
public int ProductGuideID { get; set; }
public int ProductID { get; set; }
public ProductGuide ProductGuide { get; set; }
public Product Product { get; set; }
}
编辑:ProductsController.cs:
namespace VitekAPI.Controllers
{
[Route("api/Products")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly BusinessContext _context;
public ProductsController(BusinessContext context)
{
_context = context;
}
// GET: api/Products
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
{
return await _context.Products.ToListAsync();
}
// GET: api/Products/5
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
var product = await _context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
return product;
}
// PUT: api/Products/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutProduct(int id, Product product)
{
if (id != product.ProductID)
{
return BadRequest();
}
_context.Entry(product).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Products
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<Product>> PostProduct(Product product)
{
_context.Products.Add(product);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (ProductExists(product.ProductID))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetProduct", new { id = product.ProductID }, product);
}
// DELETE: api/Products/5
[HttpDelete("{id}")]
public async Task<ActionResult<Product>> DeleteProduct(int id)
{
var product = await _context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
_context.Products.Remove(product);
await _context.SaveChangesAsync();
return product;
}
private bool ProductExists(int id)
{
return _context.Products.Any(e => e.ProductID == id);
}
}
编辑:链接到我们的 github 存储库: https ://github.com/tux-superman/VitekSky
您可以在此处查找 JSON 类型的符号。
给定您的产品类别,JSON 表示法将如下所示:
{
"ProductID": 1234,
"ProductName": "Testproduct",
"Price": 10,
"MarketID": 4321,
"Market":
{
"MarketID": 1111,
"Name": "Market1",
"Budget": 1.5,
"StartDate": 2019-11-28,
"ProductGuideID": 123
}
// All your other custom objects
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.