如何解决尝试发出POST请求时遇到的错误?
我尝试通过大摇大摆执行以下json代码:
{
"productID": 0,
"productName": "string",
"price": 0,
"marketID": 0,
"market": {
"marketID": 0,
"name": "string",
"budget": 0,
"startDate": "2019-12-10",
"productGuideID": 0,
"rowVersion": "string",
"administrator": {
"hireDate": "2019-12-10",
"productAssignments": [
null
],
"countryAssignment": {
"productGuideID": 0,
"location": "string"
},
"id": 0,
"lastName": "string",
"firstMidName": "string"
},
"products": [
null
]
},
"subscriptions": [
{
"subscriptionID": 0,
"productID": 0,
"customerID": 0,
"customerLoyalty": 0,
"customer": {
"subscriptionDate": "2019-12-10",
"subscriptions": [
null
],
"id": 0,
"lastName": "string",
"firstMidName": "string"
}
}
],
"productAssignments": [
{
"productGuideID": 0,
"productID": 0,
"productGuide": {
"hireDate": "2019-12-10",
"productAssignments": [
null
],
"countryAssignment": {
"productGuideID": 0,
"location": "string"
},
"id": 0,
"lastName": "string",
"firstMidName": "string"
}
}
]
}
执行它之后,我认为我从服务器收到400响应: 我还认为我收到200成功的反应?: 我还尝试将标准的json代码从大张旗鼓放到Postman中,然后出现404错误:
以下是我在ProductsController中的Http Post方法:
[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);
}
以下是VitekAPI项目所在的github存储库的链接: https://github.com/tux-superman/MVCandAPI
我的产品型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace VitekSky.Models
{
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; }
}
}
我的市场模型:
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; }
}
答案 0 :(得分:0)
将"string"
绑定到byte[]
时发生错误。rowVersion
是一个8字节的数组。每个字节代表一个64位整数的一部分,范围为0-255。
您无需为RowVersion
设置值,如果要创建模型,只需将其从json中删除。
此外,最好不要在没有任何更改的情况下将swagger的示例值复制为请求正文,而只是提供示例格式。
在这种情况下,您需要将[null]
替换为空。
有效的json可能是这样的:
{
"productID": 0,
"productName": "string",
"price": 0,
"marketID": 0,
"market": {
"marketID": 0,
"name": "string",
"budget": 0,
"startDate": "2019-12-10",
"productGuideID": 0,
"administrator": {
"hireDate": "2019-12-10",
"productAssignments": null,
"countryAssignment": {
"productGuideID": 0,
"location": "string"
},
"id": 0,
"lastName": "string",
"firstMidName": "string"
},
"products": null
},
"subscriptions": [
{
"subscriptionID": 0,
"productID": 0,
"customerID": 0,
"customerLoyalty": 0,
"customer": {
"subscriptionDate": "2019-12-10",
"subscriptions": null,
"id": 0,
"lastName": "string",
"firstMidName": "string"
}
}
],
"productAssignments": [
{
"productGuideID": 0,
"productID": 0,
"productGuide": {
"hireDate": "2019-12-10",
"productAssignments": null,
"countryAssignment": {
"productGuideID": 0,
"location": "string"
},
"id": 0,
"lastName": "string",
"firstMidName": "string"
}
}
]
}
答案 1 :(得分:0)
我已经遍历了您在ProductsController.cs类中的代码
我看不到您在招摇和邮递员中使用的带有“产品”和“产品” URI的发布请求
/ api /产品和 / api / Prod
这就是您面临404错误的原因
注意:如果您可以提供任何网址,我可以尝试对其进行测试