Adding a parameter on the ActionArguments

4.1k views Asked by At

I want to add a parameter on the Actionarguments, in order to bind my object, but allways the object is null and the property is not binded, why??

Maybe because that's a GET and not a POST? There's a solution for do that??

ActionFilter

public class CustomizedFilter : ActionFilterAttribute
{
    /// <summary>
    /// OnActionExecuting
    /// </summary>
    /// <param name="actionContext"></param>
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        actionContext.ActionArguments.Add("Parameter", "Value");
    }
}

Controller

    [HttpGet]
    [CustomizedFilter]
    public RestResult Get(InputObject value)
    {

    }

InputObject

        public class InputObject
        {
          public string Parameter{get;set;}
        }
1

There are 1 answers

2
Oleg Alekseenko On

That's because Model Binding is already happened. So you need to implement custom model binder for your InputObject. Or you can write something like:

public override void OnActionExecuting(HttpActionContext actionContext)
 {
  if (actionContext.ActionArguments.ContainsKey("value") && actionContext.ActionArguments["value"] is InputObject)
  {
    var val = actionContext.ActionArguments["value"] as InputObject;
    val.Parameter = "value";
  }

  base.OnActionExecuting(actionContext);
}