C# 如何通过.NET Mvc验证检查客户端是否向我发送空json数组?
我有以下视图模型:C# 如何通过.NET Mvc验证检查客户端是否向我发送空json数组?,c#,asp.net-mvc,asp.net-core-mvc,C#,Asp.net Mvc,Asp.net Core Mvc,我有以下视图模型: public class EventViewModel { [Required(ErrorMessage = "type can not be empty")] [JsonProperty("type")] [DisplayName("type")] public string Type { get; set; } [Required(ErrorMessage = "date can not be empty")] [Json
public class EventViewModel
{
[Required(ErrorMessage = "type can not be empty")]
[JsonProperty("type")]
[DisplayName("type")]
public string Type { get; set; }
[Required(ErrorMessage = "date can not be empty")]
[JsonProperty("date")]
[DisplayName("date")]
public int Timestamp { get; set; }
[JsonProperty("data")]
public JObject Data { get; set; }
}
以及以下控制器操作:
[Route("/api/v1.0/events")]
[HttpPost]
public async Task<IActionResult> Events([FromBody] List<EventViewModel> viewModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
但是如果一个客户只发送给我:
[]
空数组
我想显示验证消息。我怎么能做到?可能吗
使现代化
也许问题的解释不是很清楚,或者我不明白什么。我会试着把它修好
如果我有这种JSON:
{
"items" :[
{
...
},
...
]
}
那就好了。没问题。我可以写:
public class EventViewModelList
{
[Required]
List<EventViewModel> EventViewModels {get; set;}
}
但我的JSON只是一个对象数组。所以,我不能
我可以做一些事情,比如:
public async Task<IActionResult> Events([Required][FromBody] List<EventViewModel> viewModel)
因为它不起作用。
将验证放入控制器?无控制器-是控制器。验证-是验证
我想我需要属性。但是控制器操作级别属性。比如:
[Route("/api/v1.0/events")]
[NotEmptyListOfViewModels]
[HttpPost]
public async Task<IActionResult> Events([FromBody] List<EventViewModel> viewModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
我说得对吗?你可以做如下事情:
[Route("/api/v1.0/events")]
[HttpPost]
public async Task<IActionResult> Events([FromBody] List<EventViewModel> viewModel)
{
if(viewModel == null || viewModel.Count == 0)
{
ModelState.AddModelError("","List can not be empty or null");
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
..............
}
您可以编写自定义模型活页夹,如下所示: NotEmptyListOfViewModels 事件行动
您可以通过实现ValidationAttribute来创建自己的验证属性,并将该属性添加到属性中。这是否回答了您的问题?
[Route("/api/v1.0/events")]
[HttpPost]
public async Task<IActionResult> Events([FromBody] List<EventViewModel> viewModel)
{
if(viewModel == null || viewModel.Count == 0)
{
ModelState.AddModelError("","List can not be empty or null");
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
..............
}
public class NotEmptyListOfViewModels:IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
//Get command model payload (JSON) from the body
String valueFromBody;
using (var streamReader = new StreamReader(bindingContext.HttpContext.Request.Body))
{
valueFromBody =await streamReader.ReadToEndAsync();
}
var modelInstance = JsonConvert.DeserializeObject<List<EventViewModel>>(valueFromBody);
if(modelInstance.Count==0)
{
bindingContext.ModelState.AddModelError("JsonData", "The json is null !");
}
bindingContext.Result = ModelBindingResult.Success(modelInstance);
}
}
[Route("/api/v1.0/events")]
[HttpPost]
public async Task<IActionResult> Events([ModelBinder(BinderType = typeof(NotEmptyListOfViewModels))]List<EventViewModel> viewModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok();
}