skip to Main Content

It has been a day trying to figure out how to pass parameters from action filter to the controllers actions but with no luck. I have been to this SO question and tried both of the answers.
When I debug my application I don’t get a value for my controller action parameter it is always null.

Here is the action filter…

public class FacebookCheckPermission : ActionFilterAttribute
    {
        public string redirectURL { get; set; }
        public async  override void OnActionExecuting(ActionExecutingContext filterContext)
        {
                var myBaseController = (BaseController)filterContext.Controller;
                PermissionRequestViewModel permissionViewModel = myBaseController.GetMissingPermissions();

                filterContext.RouteData.Values.Add("Value1", "Hi");

                filterContext.ActionParameters["Value2"] = "Bye";

                if (permissionViewModel != null &&
                    permissionViewModel.MissingPermissions.Count > 0)
                //Code removed for brevity.

                base.OnActionExecuting(filterContext);
        }

    }

And here is the Controller Action:

[FacebookCheckPermission]
public async Task<ActionResult> Index(string Value1, string Value2)
{

UPDATE
Seems that I have found the problem. The problem is that my Action filter is async and I have some API calls to facebook. I can get my action filter parameter if I declare it anywhere before the API calls but when I declare it under an API call my action parameter become null. What could be the problem?

Here is the call to facebook graph API.
dynamic myInfo = await fb.GetTaskAsync("me?fields=id,first_name,last_name,link,locale,email,name,birthday,gender,location,age_range,about".GraphAPICall(appsecret_proof));

2

Answers


  1. Use ActionParameters

     public class FacebookCheckPermission : ActionFilterAttribute
        {
            public string redirectURL { get; set; }
    
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                filterContext.ActionParameters["Value1"] = "Hi";
                filterContext.ActionParameters["Value2"] = "Bye";
    
                base.OnActionExecuting(filterContext);
            }
    
        }
    
            [FacebookCheckPermission]
            public Task<ActionResult> Index(string Value1, string Value2)
            {
                string lol = $"{Value1} - {Value2}";
                return null;
    
            }
    

    Use simple adding ActionParameters first, that works fine, then add other codes, there is not any problem with ActionParameters.

    Login or Signup to reply.
  2. You can also use RouteData values.

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.RouteData.Values.Add("MyParam", "ParamValue");
    }
    

    Then in the controller.

       var paramValue = RouteData.Values["MyParam"].ToString();
    

    There is a “clunkier” method… I’ve put a private var at the top of the controller, assigned to this in OnActionExecuting and then used that var in my action method.

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search