70

If I create an object in a Custom Action Filter in ASP.NET MVC in

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    DetachedCriteria criteria = DetachedCriteria.For<Person>();
    criteria.Add("stuff");

    // Now I need to access 'criteria' from the Action.....

}

is there any way I can access the object from the Action that is currently executing.

4 Answers 4

65

The better approach is described by Phil Haack.

Basically this is what you do:

public class AddActionParameterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        // Create integer parameter.
        filterContext.ActionParameters["number"] = 123;

        // Create object parameter.
        filterContext.ActionParameters["person"] = new Person("John", "Smith");
    }
}

The only gotcha is that if you are creating object parameters, then your class (in this case Person) must have a default constructor, otherwise you will get an exception.

Here's how you'd use the above filter:

[AddActionParameter]
public ActionResult Index(int number, Person person)
{
    // Now you can use number and person variables.
    return View();
}
7
  • 2
    Very nice way to do this, I definitely think this is way better than the accepted answer. This should be the accepted answer! Thanks!
    – avb
    Jul 24, 2014 at 12:02
  • Why is this better than the accepted answer? There seems to be no reason why this would be the case and the additional [AddActionParameter] is clearly additional work that the other method doesn't require to function.
    – Chris
    Mar 1, 2016 at 9:57
  • @ChrisBertrand It's cleaner and avoids compile-time errors in controller, by using strong-typing, i.e. - Person class. It also makes unit-testing much easier.
    – niaher
    Mar 1, 2016 at 11:25
  • 1
    The only problem with this approach is when you're trying to do this and a post at the same. By adding it to the action parameters, it then participates in model validation, and you can end up with odd situations where the posted data is valid, but ModelState.IsValid is still false, because something's not valid on the object you passed in. Apr 22, 2016 at 13:01
  • 6
    This isn't a great solution because now your api could accept these parameters as querystrings which is an unintended side-effect. It's worse when you have documentation libraries like swagger which will show these parameters are query parameters for the URL when they're strictly for internal use.
    – arviman
    Jun 1, 2016 at 9:22
56

I would recommend putting it in the Route data.

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.RouteData.Values.Add("test", "TESTING");
        base.OnActionExecuting(filterContext);
    }

    public ActionResult Index()
    {
        ViewData["Message"] = RouteData.Values["test"];

        return View();
    }
4
  • 2
    How long does an item survive for in RouteData? I only need to keep the object for the duration of the currently executing action, or at the most for the current request, if this is how route data works then this is the answer otherwise HttpContext.Items is probably better. Nov 29, 2009 at 12:07
  • 1
    RouteData is data related to the currently executing route (action). Think it as a container that represents the request url parsed and mapped according to your routing rules.
    – Neal
    Jan 13, 2010 at 2:09
  • RouteData is certainly great for doing this as I've learned today thanks to your answer here. My beautiful black & yellow MVC book here in front of me (fourth edition) doesn't mention anything about it or do anything similar to this at all in the entire chapter on filters (or at least I just haven't found it yet?). Anyway, +1 and thank you!
    – Funka
    Mar 7, 2013 at 2:25
  • filterContext.ControllerContext.RouteData.Values.Add("key",value); is working for me! :)
    – Nameless
    Mar 24, 2017 at 10:50
43

You could use the HttpContext:

filterContext.HttpContext.Items["criteria"] = criteria;

And you can read it in the action:

[YourActionFilter]
public ActionResult SomeAction() 
{
    var criteria = HttpContext.Items["criteria"] as DetachedCriteria;
}
1
  • 1
    I was thinking about using HttpContext.Items[] and its an acceptable solution as it will be cleared out at the end of the request. I wasn't sure if there was a place I could store stuff that exists only for the duration of the action. Nov 29, 2009 at 12:08
3

Set item in ViewData or of a viewmodel if you pass it as a parameter into your action. Here I set the property of a ViewModel

public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     ViewModelBase viewModel = null;
     foreach (object parameter in filterContext.ActionParameters.Values)
     {
         if (parameter is ViewModelBase)
         {
             viewModel = (ViewModelBase)parameter;
             break;
         }
     }
     if(viewModel !=null)
     {
         viewModel.SomeProperty = "SomeValue";
     }
 }


    public ActionResult About(ViewModelBase model)
    {
      string someProperty= model.SomeProperty;
}

Here is the untyped version I think you prefer:

   public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData.Add("TestValue", "test");

    }

       [FilterWhichSetsValue]
        public ActionResult About()
        {
            string test = (string)ViewData["TestValue"];
            return View();
        }
2
  • Thanks for your suggestion. My actions don't take ViewModelBase as a parameter though, and I would prefer not to introduce it just to solve my problem. Nov 29, 2009 at 12:10
  • See the edited post for the untyped version. I would still use the first version. Maybe the parametername just sounds to bad. It can be any class and doesnt have to be the class of the typed view.
    – Mathias F
    Nov 29, 2009 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.