Pass argument to a Controller from Action Filter
Asked Answered
A

3

14

Can I pass an argument from Action Filter to a controller and specifically through the parameters (in ASP.NET Core) ?

For example:

public class CategoryController : Controller
{
    [HttpGet]
    [ServiceFilter(typeof(ApiFilter))]
    public async Task<IActionResult> Index(string dataComingFromActionFilter)
    {
        //use dataComingFromActionFilter
    }
}

And

public class ApiFilter: IActionFilter 
{
    public void OnActionExecuting(ActionExecutingContext context)
    {
        //maybe something similar to
        context.sendToActionAsArgument['dataComingFromActionFilter'] = "data";

    }
}
Apocryphal answered 2/1, 2019 at 20:3 Comment(9)
Look promising: learn.microsoft.com/en-us/dotnet/api/…Fancher
Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP COREApocryphal
Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?Fancher
Yes the controller is available in the context, How can I manipulate or set the values passed as arguments through this controller contextApocryphal
RouteData or any function that changes the arguments & is callable from the Action Filter Context.Apocryphal
The objective is to receive the values in the action method and through the parametersApocryphal
Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.Fancher
That's really smart! but I have one concern: Tasks are being called asynchronously. and if this controller gets 2 requests at same time. What would happen to this property ?Apocryphal
Each request has its own unique context.Yippie
T
16

you can use

 context.ActionArguments["dataComingFromActionFilter"] = "data";

for updating existing action parameter. If you need to add new parameter, then

context.ActionArguments.Add("dataComingFromActionFilter", "data"); 

then you can reach it through Controller Action

Tisdale answered 2/1, 2019 at 22:52 Comment(1)
Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?Apocryphal
K
6

In the attribute set values into Items collection of HttpContext:

context.HttpContext.Items.Add("DataFromAttribute", date);

And In the controller read them in the same way:

this.HttpContext.Items["DataFromAttribute"];
King answered 7/11, 2019 at 16:33 Comment(0)
D
0

Try with code the below to see It work.

public class PersonFilterAttribute: Attribute, IAsyncActionFilter
    {
        public PersonFilterAttribute()
        {
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
          context.ActionArguments.Add("personService", new PersonService("Thomas", 29,"Miami");       
          await next();
        }
    }
    
And In the controller read them in the same way:

this.HttpContext.Items["personService"];
Dorie answered 26/7, 2021 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.