简体   繁体   English

从 Netcore 2.2 Web API 的响应中获取错误消息

[英]Get ErrorMessage from Response in Netcore 2.2 Web API

I call Register method with empty username and password.我用空的用户名和密码调用 Register 方法。 So I received this result:所以我收到了这个结果:

{
    "errors": {
        "Password": [
            "The Password field is required.",
            "Password length is between 4 and 8."
        ],
        "Username": [
            "The Username field is required."
        ]
    },
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "0HLJIO56EGJEV:00000001"
}

My Dto:我的 Dto:

public class UserForRegisterDto
{
    [Required]
    public string Username { get; set; }
    [Required]
    [StringLength(8, MinimumLength = 4, ErrorMessage = "Password length is between 4 and 8.")]
    public string Password { get; set; }
}

I only want to get errors attribute from response, What should I do?我只想从响应中获取错误属性,我该怎么办?

This is a new feature in ASP.NET Core 2.2:这是 ASP.NET Core 2.2 中的一项新功能

An IActionResult returning a client error status code (4xx) now returns a ProblemDetails body.一个IActionResult返回客户端错误状态代码(4XX)现在返回一个ProblemDetails体。

The docs describe that this can be disabled when calling AddMvc inside of ConfigureServices , like this: 文档描述了在ConfigureServices内部调用AddMvc时可以禁用此功能,如下所示:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
    });

This will result in the pre-2.2 behavior, which will serialise only the errors.这将导致 2.2 之前的行为,它只会序列化错误。

As I went through a very close scenario, I will add here an answer just in case others run into the same or similar issues:当我经历了一个非常接近的场景时,我会在这里添加一个答案,以防其他人遇到相同或类似的问题:

In my case, the problem involved a project with .NET Core 3.1 as Target Framework.就我而言,问题涉及一个以 .NET Core 3.1 作为目标框架的项目。 My project had a NuGet package dll as one of its dependencies and such package had several models.我的项目有一个 NuGet 包 dll 作为它的依赖项之一,这样的包有几个模型。 Our controller endpoints had these models as parameters.我们的控制器端点将这些模型作为参数。 However, requests sent to any of these endpoints (with any of those models as body) were giving rise to validation errors for non-nullable reference type properties.但是,发送到这些端点中的任何一个(以这些模型中的任何一个作为主体)的请求都会导致不可为空的引用类型属性的验证错误。

The workaround to solve it was similar to the one suggested by Kirk.解决此问题的解决方法与 Kirk 建议的方法类似。 I have added the following option to AddControllers method, from Startup's ConfigureServices method:我在 Startup 的 ConfigureServices 方法中向 AddControllers 方法添加了以下选项:

services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

Note that, by using this workaround, it will be necessary to manually add [Required] attributes for nullable reference type properties around your application.请注意,通过使用此解决方法,需要为应用程序周围的可为空引用类型属性手动添加 [Required] 属性。

I also opened an issue on their repository: https://github.com/dotnet/aspnetcore/issues/27448我还在他们的存储库中打开了一个问题: https : //github.com/dotnet/aspnetcore/issues/27448

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM