How to automate the validation of methods with parameters using asp.net core?

Viewed 25

I have a asp.net core 3.1 web api code base. There are many methods without using DTO classes as their method parameters and instead of that they are using single parameters as mentioned in the example below:

    [HttpGet("{reviewId:long:min(1)}")]
    [ProducesResponseType(400)]
    [ProducesResponseType(500)]
    public async Task<IActionResult> GetDetails(long reviewId)
    {
        if (reviewId <= 0)
        {
            return BadRequest("Supply valid engagement profile Id");
        }

        var result = await _testService.GetDetails(reviewId);
        return Ok(result);
    }

There are many such methods in the application having single parameter. Now the problem here is the there is a repetition of logic to check whether the input parameter is always greater than 0.

I wanted to put a single filter and remove the repetition of code.

I have already added two global filters 1. CheckModelForNullAttribute and 2.ValidateModelAttribute , but these are applied on the methods with POCO classes as their input parameters. I wanted to do something similar for the methods with single parameters too.

Can anyone help me to resolve this issue?

0 Answers
Related