[英]ASP.Net Core Put FromBody request validation - json request contains only one bool field
我必须确保为 PUT 端点提交的请求包含一个也是唯一一个 bool 类型的元素。 请求采用 Json 格式,如下所示,可能值为真或假。
{
"canRegister": true
}
下面是C#请求object和Controller中对应的方法(端点)。 公共 class 注册请求查看模型
{
[Required]
[JsonPropertyName("canRegister")]
[JsonProperty(Required = Required.Always)]
[Range(typeof(bool), "false", "true", ErrorMessage = "false or true are only allowed values")]
public bool CanRegister { get; set; }
}
public IActionResult Put([FromBody]RegistrationRequestViewModel request)
虽然当请求包含“canRegister”时这完全符合我的预期,但我面临的问题是当请求不包含“canRegister”时,C# 的默认序列化仍然导致请求 object 包含“canRegister”且为 false价值。
谁能告诉我如何确保这个不正确的 Json 转换不会发生? 我尝试使用自定义 ActionFilter 并意识到在调用 OnActionExecuting 方法时发生了不正确的转换。
[JsonProperty(Required = Required.Always)]
在Json.Net中使用。自Z1848E732214CCE460F9B24F0C80F5073E核心共享框架ASP.NET核心共享框架中移除。
为了满足您的要求,您可以添加 NewtonSoft 支持:
1.安装Microsoft.AspNetCore.Mvc.NewtonsoftJson
package。
2.更新Startup.ConfigureServices
以调用AddNewtonsoftJson
。
services.AddControllers()
.AddNewtonsoftJson();
3.更新您的 model:
public class RegistrationRequestViewModel
{
[Required]
//[JsonPropertyName("canRegister")]
[Newtonsoft.Json.JsonProperty(Required = Newtonsoft.Json.Required.Always,PropertyName = "canRegister")]
[Range(typeof(bool), "false", "true", ErrorMessage = "false or true are only allowed values")]
public bool CanRegister { get; set; }
public string Title { get; set; }
}
结果:
如果您仍想使用System.Text.Json
,您可以自定义 JsonConverter:
public class RegistrationRequestViewModelJsonConverter : JsonConverter<RegistrationRequestViewModel>
{
public override bool CanConvert(Type typeToConvert)
{
return base.CanConvert(typeToConvert);
}
public override RegistrationRequestViewModel Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
var flag = false;
var model = new RegistrationRequestViewModel();
List<string> list = new List<string>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
string propertyName = reader.GetString();
list.Add(propertyName);
reader.Read();
switch (propertyName)
{
//canRegister and title is the key name you post in json
case "canRegister":
bool canRegister = reader.GetBoolean();
model.CanRegister = canRegister;
flag = true;
break;
case "title":
string title = reader.GetString();
model.Title = title;
flag = true;
break;
}
}
}
if (!list.Contains("canRegister"))
{
throw new JsonException("CanRegister field must be provided");
}
return model;
}
public override void Write(
Utf8JsonWriter writer,
RegistrationRequestViewModel value,
JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
Model:
[JsonConverter(typeof(RegistrationRequestViewModelJsonConverter))]
public class RegistrationRequestViewModel
{
[Required]
[JsonPropertyName("canRegister")]
// [JsonProperty(Required = Required.Always)]
[Range(typeof(bool), "false", "true", ErrorMessage = "false or true are only allowed values")]
public bool CanRegister { get; set; }
public string Title { get; set; }
}
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.