简体   繁体   中英

Handling ASP.NET Core Web API object property binding

I have an ASP.NET Core 2.1 Web API application. If I send to API endpoint empty quotes as json object property value in request body I`ll have that property been initialized with null (it is of type Guid?). How can I change such behavior? What I need is just send BadRequest (400) as response error code.

That is an endpoint:

public async Task<ActionResult<ExampleResponse>> ExamplePatch([FromBody] ExampleModel exampleModel,
        CancellationToken cancellationToken)
{
    /* At this point exampleModel.ExampleGuid is initilized with null. So it should be handled before */
    /* some processing */
    return StatusCode(StatusCodes.Status201Created, ExampleResponse);
}

Here is the model:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.10.53.0 (Newtonsoft.Json v9.0.0.0)")]
public class ExampleModel 
{
    /* a bunch of properties here */
    [Newtonsoft.Json.JsonProperty("exampleGuid", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public System.Guid? ExampleGuid { get; set; }
}

Json from request body:

{ ... other parameters here ..., "exampleGuid": "" }

I have one restriction on that - I cannot add attributes to that model property or change the model somehow. I`ll be very appreciated for any help!

You can create a Validator for the ExampleModel with FluentValidation package. Here is the example:

public class ExampleModelValidator : AbstractValidator<ExampleModel>
    {
        public ExampleModelValidator()
        {
            RuleFor(e => e.ExampleGuid)
                .Must(guid => guid.ToString().lenght > 0)
                 .When(e => e.ExampleGuid.HasValue)
                 .WithMessage("Must be a valid GUID");

        }
    }

This will validate your model before the endpoint body.

Additionaly I personally like to add this piece of code in my Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services
            
            .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<ExampleModelValidator>())
            .ConfigureApiBehaviorOptions(options =>
            {
                options.InvalidModelStateResponseFactory = context =>
                {
                    var errorsList = string.Join(" | ", context.ModelState.Values.SelectMany(v => v.Errors).Select(err => err.ErrorMessage).ToArray());
                    return new BadRequestObjectResult(errorsList);
                };
            });
    }

This will return all validation messages separated by pipe and BadRequest as response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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