我有一个 ASP.NET Core Web API,带有
POST
方法,该方法接受 JSON 作为请求。仅当我发送特定字符串值时,服务才有效。我发送content-type = json
。
注意:如果我发送 status = NEW/QUEUE 它可以工作,但我发送空字符串它会失败。
此操作失败 - 返回 http 400 Bad Request - 发送请求时发生错误:
{
"orderId": 0,
"customerId": 999999,
"orderNumber": "12345",
"orderTypeId": 5,
"status": ""
}
这有效并返回 http 200 成功:
{
"orderId": 0,
"customerId": 999999,
"orderNumber": "12345",
"orderTypeId": 5,
"status": "**NEW**"
}
这也有效并返回 http 200 成功:
{
"orderId": 0,
"customerId": 999999,
"orderNumber": "12345",
"orderTypeId": 5,
"status": "**QUEUE**"
}
由于
status
是一个字符串,因此它应该允许空值。 API 端也没有验证来阻止它。
这应该返回 200 Success
{
"orderId": 0,
"customerId": 999999,
"orderNumber": "12345",
"orderTypeId": 5,
"status": ""
}
namespace Sample.DT
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
public class Orders
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
[Required(ErrorMessage = "Order number is required")]
[StringLength(15, ErrorMessage = "Please enter valid order number")]
public string OrderNumber { get; set; }
[Required]
public int OrderTypeId { get; set; }
public string Status { get; set; } = "";
}
}