0

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));

Dawar
  • 99
  • 3
  • 14
  • That is just for example the real string names are different from ones I written here any ways I'll change it – Dawar May 25 '17 at 13:18
  • @Igor Thanks for the correction :) – Dawar May 25 '17 at 13:21
  • In update you ask another question, I suggest check the answers if that useful for your question accept it, and ask another question with proper title and body, that's the way Stackoverflow recommend. – Saeid May 25 '17 at 16:58

2 Answers2

0

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.

Wheels73
  • 2,850
  • 1
  • 11
  • 20
-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.

Saeid
  • 13,224
  • 32
  • 107
  • 173
  • What is the $ sign? I never used it in MVC? – Dawar May 25 '17 at 13:41
  • 2
    @Dawar It is C# 6 feature for string.format() you can use string.forma() I just want to check the values passed correct - string lol = string.Format("{0} - {1}", Value1, Value2); – Saeid May 25 '17 at 13:42
  • But if the parameter values become null how can I access those values inside the action? – Dawar May 25 '17 at 13:44
  • @Dawar When you use filterContext.ActionParameters["Value1"] = "Hi"; in ActionFilter that is not null, just in case that you passed the null value like: filterContext.ActionParameters["Value1"] = null; – Saeid May 25 '17 at 13:48
  • Saeid, The question is updated please have a look and tell me if you can help. – Dawar May 25 '17 at 16:30