简体   繁体   English

Model state 马上回归.Net Core 5

[英]Model state returns right away .Net Core 5

I have a custom validation attribute我有一个自定义验证属性

public class IsUsernameAcceptableAttr:ValidationAttribute
{
    
    protected override ValidationResult IsValid(object username, ValidationContext validationContext)
    {
        var _username = username.ToString();
        if (_username.Length<4)
            return new ValidationResult("Username must be at least 4 characters.");
       
        return ValidationResult.Success;

    }
}

in my DTO class I have在我的 DTO class 我有

 public class UserAccountInformationDTO
    {
        [IsUsernameAcceptableAttr]
        public string Username { get; set; }


    }

In action method I got在行动方法我得到

public IActionResult UpdateAccountInformationstring(UserAccountInformationDTO accountInfo)
    {
        var ms = ModelState.IsValid;
        return Ok();
        
    }

The issue is when model state hits an error it send a response back in errors as JSON response.问题是当 model state 遇到错误时,它会以 JSON 响应的形式返回errors响应。 I never get to do anything in the controller or alter the response to a format that i want.我从来没有在 controller 中做任何事情,或者将响应更改为我想要的格式。 What else can i do?我还可以做些什么?

You need to specify the source of the parameters您需要指定参数的来源

https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-5.0#binding-source-parameter-inference https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-5.0#binding-source-parameter-inference

Update:更新:

Add the following codes to your startup ConfigureServices:将以下代码添加到您的启动 ConfigureServices:

services.Configure<ApiBehaviorOptions>(apiBehaviorOptions => {
    apiBehaviorOptions.SuppressModelStateInvalidFilter = true;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET WEBAPI核心中的模型状态验证 - Model State Validation in ASP.NET WEBAPI Core 使用 ViewModel 的 Model State 无效 - C# Z9E0DA8438E1E38A1C30F4B76CE8B76CE - Invalid Model State with ViewModel - C# ASP.NET CORE ASP.NET 核心连接两个表 model 返回 NullReferenceException - ASP.NET Core connect two table model returns NullReferenceException ASP.NET Core 6 MVC 项目标识 model 返回空 - ASP.NET Core 6 MVC project identity model returns empty 通过ReceiveAsync处理Azure队列立即返回null - Azure queue handling via ReceiveAsync returns null right away ASP.NET Core Razor页面模型状态无效且模型数据为空 - ASP.NET Core Razor Pages Model State Is Not Valid & Model data is empty 如何检查子模型 state 是否有效,而不是检查 asp.net 核心中的主要 model state? - How can the check sub-model state is valid or not instead of checking the main model state in asp.net core? 如果我离开页面并返回,模型返回null - Model returns null if I navigate away from page and come back How can you maintain model state in ASP.NET Core between repeatedly displaying the Model in a View and sending the model back to the Controller? - How can you maintain model state in ASP.NET Core between repeatedly displaying the Model in a View and sending the model back to the Controller? 如何防止ASP.NET Core在空请求主体上将模型状态标记为无效? - How can I prevent ASP.NET Core from marking model state as invalid on empty request bodies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM