Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

c# - Intercept bad requests before reaching controller in ASP.NET Core

I have a logic to apply in case the request received is a BadRequest, to do this I have created a filter:

public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            // Apply logic
        }
    }
}

In Startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options => { options.Filters.Add<ValidateModelAttribute>(); });
}

Controller:

[Route("api/[controller]")]
[ApiController]
public class VerifyController : ControllerBase
{
    [Route("test")]
    [HttpPost]
    [ValidateModel]
    public ActionResult<Guid> validationTest(PersonalInfo personalInfo)
    {
        return null;
    }
}

Model:

public class PersonalInfo
{
    public string FirstName { get; set; }
    [RegularExpression("\d{4}-?\d{2}-?\d{2}", ErrorMessage = "Date must be properly formatted according to ISO 8601")]
    public string BirthDate { get; set; }
}

The thing is when I put a break point on the line:

if (!context.ModelState.IsValid)

execution reaches this line only if the request I send is valid. Why it is not passing the filter if I send a bad request?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The [ApiController] attribute that you've applied to your controller adds Automatic HTTP 400 Responses to the MVC pipeline, which means that your custom filter and action aren't executed if ModelState is invalid.

I see a few options for affecting how this works:

  1. Remove the [ApiController] attribute

    Although you can just remove the [ApiController] attribute, this would also cause the loss of some of the other features it provides, such as Binding source parameter inference.

  2. Disable only the Automatic HTTP 400 Responses

    Here's an example from the docs that shows how to disable just this feature:

    services.AddControllers()
        .ConfigureApiBehaviorOptions(options =>
        {
            // ...
            options.SuppressModelStateInvalidFilter = true;
            // ...
        }
    

    This code goes inside of your Startup's ConfigureServices method.

  3. Customise the automatic response that gets generated

    If you just want to provide a custom response to the caller, you can customise what gets returned. I've already described how this works in another answer, here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...