Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating and passing controller-level parameters with ASP.NET MVC attribute routing

I have an ASP.NET controller where every single method will have a shared parameter. With attribute routing, I can add this parameter in the controller's route.

However, I still need to add that parameter along with a validation attribute in every single method. Is there a way for me to do the validation in one place or avoid having to pass it in to every single method?

This is the current working code:

[ApiController]
[Route("[controller]/{name}")]
public class ExampleController : ControllerBase
{
    [HttpGet]
    public string Sample([StringLength(10)][FromRoute]string name)
    {
    }

    [HttpGet]
    [Route("defaults")]
    public string GetDefaults([StringLength(10)][FromRoute]string name)
    {
    }

    [HttpGet]
    [Route("objects/{id}")]
    public string Sample([StringLength(10)][FromRoute]string name, [FromRoute]string id)
    {
    }
}

Is it possible to get something close to this? (I know the validation parameter on the controller is invalid, but I'd like to just have to apply it once)

[ApiController]
[StringLength(10)]
[Route("[controller]/{name}")]
public class ExampleController : ControllerBase
{
    [HttpGet]
    public string Sample()
    {
    }

    [HttpGet]
    [Route("defaults")]
    public string GetDefaults()
    {
    }

    [HttpGet]
    [Route("objects/{id}")]
    public string Sample([FromRoute]string id)
    {
    }
}
like image 773
Wieschie Avatar asked Dec 18 '22 15:12

Wieschie


2 Answers

You can do this using a custom action filter to validate the name parameter:

public class ValidateNameParameterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(name))
        {
            // the trick is to get the parameter from filter context
            string name = filterContext.ActionParameters[name] as string;

            // validate name

            if (/*name is not valid*/)
            {
                // you may want to redirect user to error page when input parameter is not valid
                filterContext.Result = new RedirectResult(/*urlToRedirectForError*/);
            }

            base.OnActionExecuted(filterContext);
        }
    }
}

Now you can apply the filter to your controller, or specific actions:

[ApiController]
[Route("[controller]/{name}")]
[ValidateNameParameter] // <-- execute this for all actions in the controller
public class ExampleController : ControllerBase
{
    [HttpGet]
    public string Sample([StringLength(10)][FromRoute]string name)
    {
    }

    [HttpGet]
    [Route("defaults")]
    public string GetDefaults([StringLength(10)][FromRoute]string name)
    {
    }

    [HttpGet]
    [Route("objects/{id}")]
    // [ValidateNameParameter] // <-- execute for this specific action
    public string Sample([StringLength(10)][FromRoute]string name, [FromRoute]string id)
    {
    }
}

See this tutorial for more information.

like image 200
Hooman Bahreini Avatar answered Dec 20 '22 04:12

Hooman Bahreini


As Hooman Bahreini said , you could customize a action filter that inherits ActionFilterAttribute and use it as a attribute on the controller .In Asp.net core , ActionArguments replaces ActionParameters

public class ValidateNameParameterAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionArguments.ContainsKey("name"))
        {
            string name = filterContext.ActionArguments["name"] as string;

            if(name!=null && name.Length>10)
            {
                filterContext.Result = new BadRequestObjectResult("The length of name must not exceed 10");
            }
        }
    }
}

For more details on Filters in ASP.NET Core , you could refer to here .

like image 30
Xueli Chen Avatar answered Dec 20 '22 05:12

Xueli Chen