ASP.Net Core验证问题状态-绑定验证未返回问题详细信息

时间:2018-10-07 18:02:54

标签: c# asp.net-core asp.net-core-2.1

同一问题在此处发布:https://github.com/aspnet/Mvc/issues/8564

我遇到一个问题,当执行被控制器执行时,我的代码显式返回ValidationProblemDetails响应。

但是,当绑定验证阻止执行进入控制器时,我得到以下JSON响应(标准模型状态验证对象)。

{
  "Email": [
    "Invalid email address"
  ]
}

为什么它没有在响应中返回验证问题详细信息?

我正在使用Microsoft.AspNetCore.App 2.1.4软件包。

请求模型

public class RegistrationRequest
{
    [Description("Given Name")]
    [MaxLength(100)]
    [Required(ErrorMessage = "Given Name is required")]
    public string GivenName { get; set; }

    [MaxLength(100)]
    [Required(ErrorMessage = "Surname is required")]
    public string Surname { get; set; }

    [MaxLength(255)]
    [Required(ErrorMessage = "Email is required")]
    [EmailAddress(ErrorMessage = "Invalid email address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Password is required")]
    public string Password { get; set; }

    [Description("Confirm Password")]
    [Compare(nameof(Password), ErrorMessage = "Passwords do not match")]
    public string ConfirmPassword { get; set; }
}

启动

public class Startup
{
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        ...
        services.Configure<ApiBehaviorOptions>(options =>
        {
            options.InvalidModelStateResponseFactory = context =>
            {
                var problemDetails = new ValidationProblemDetails(context.ModelState)
                {
                    Instance = context.HttpContext.Request.Path,
                    Status = (int)HttpStatusCode.BadRequest,
                    Detail = "Please refer to the errors property for additional details"
                };

                return new BadRequestObjectResult(problemDetails)
                {
                    ContentTypes = "applicaton/json"
                };
            };
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        ...
    }
}

控制器

[ApiController]
[Authorize]
[Route("users")]
public sealed class UserController : Controller
{
    public UserController(
        UserManager userManager,
        IMapper mappingProvider)
    {
        Manager = userManager;
        Mapper = mappingProvider;
    }

    private UserManager Manager { get; }
    private IMapper Mapper { get; }

    [HttpPost]
    [AllowAnonymous]
    [Consumes("application/json")]
    [Produces("application/json")]
    [ProducesResponseType(200)]
    [ProducesResponseType(400, Type = typeof(ValidationProblemDetails))]
    public async Task<IActionResult> Post([FromBody]ApiModels.RegistrationRequest request)
    {
        if (request == null) throw new ArgumentNullException(nameof(request));

        var user = Mapper.Map<DataModels.User>(request);

        var result = await Manager.Create(user, request.Password); // return OperationResult

        return result.ToActionResult();
    }
}

扩展方法,可将OperationResult转换为IActionResult

public static class OperationResultExtensions
{
    public static ValidationProblemDetails ToProblemDetails(this OperationResult result)
    {
        if (result == null) throw new ArgumentNullException(nameof(result));

        var problemDetails = new ValidationProblemDetails()
        {
            Status = (int)HttpStatusCode.BadRequest
        };

        if (problemDetails.Errors != null)
        {
            result.Errors
               .ToList()
               .ForEach(i => problemDetails.Errors.Add(i.Key, i.Value.ToArray()));
        }

        return problemDetails;
    }

    public static IActionResult ToActionResult(this OperationResult result)
    {
        switch (result.Status)
        {
            case HttpStatusCode.OK:
                return new OkResult();

            case HttpStatusCode.NotFound:
                return new NotFoundResult();

            case HttpStatusCode.BadRequest:
                var problems = result.ToProblemDetails();
                return new BadRequestObjectResult(problems);

            default:
                return new StatusCodeResult((int)result.Status);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您有String fontName = "OldStyle 1 HPLHS" 之前 Configure<ApiBehaviorOptions>:对AddMvc的调用注册了一个implements IConfigureOptions<ApiBehaviorOptions>的类,最终覆盖了您的实例已使用AddMvc进行了配置,并且有效地resets the factory function进行了配置。

要完成此任务,您需要做的就是在services.Configure<ApiBehaviorOptions>中切换顺序:

ConfigureServices

我还注意到,尽管它并不是完全粗体,但docs暗示此顺序也很重要:

  

在services.AddMvc()。SetCompatibilityVersion(CompatibilityVersion.Version_2_1)之后,在Startup.ConfigureServices中添加以下代码。

     

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.Configure<ApiBehaviorOptions>(options => { options.InvalidModelStateResponseFactory = context => { ... }; });