Gaz
Gaz

Reputation: 4154

Accessing objects created by ModelBinders in ActionFilters

I'm not sure if this is possible but I want to see objects created by my ModelBinders other than having them passed as parameters to my Action methods.

I.e. I want to register a FooBinder and a BarBinder, then look at a Foo in the following method

public void MyAction(Bar bar)

or even ideally in an ActionFilter.

Is this possible?

Upvotes: 0

Views: 99

Answers (1)

Arnis Lapsa
Arnis Lapsa

Reputation: 47587

To access this:

 public ActionResult FizzAction(object foo) // <--
    {...}

Use this in your filter:

public class BarFilter : ActionFilterAttribute
{
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     var foo = filterContext.ActionParameters["foo"];
     //do whatever you want with it
 }
}

EDIT:

For ActionMethodSelectorAttribute this might help:

 public class foo : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest
            (ControllerContext controllerContext, MethodInfo methodInfo)
        {
            ValueProviderResult valueResult;
            controllerContext.Controller.ValueProvider  
                .TryGetValue("foo", out valueResult);
        }
    }

Check out this blog post by K. Scott Allen.

No warranty - haven't used this by myself - just found through watch window. :)

Upvotes: 2

Related Questions