Click here to Skip to main content
11,502,821 members (49,102 online)
Click here to Skip to main content

Tagged as

How to change the input parameters using action filters

, 29 Aug 2012 CPOL 9K 1
Rate this:
Please Sign up or sign in to vote.
How we can change the request parameters passed to an action in action filters.

Important: This article is written to demonstrate how we can change the request parameters passed to an action in action filters. But if the concern is how to customize binding the request values to action parameters then the better way is to go for custom model binders.

Action Filters

Filters are behaviors that can be added to different stages in the ASP.NET MVC request processing pipeline. There are four different types of filters: Authorization, Action, Result and Exception. The Action filters are the ones that are called before and after an action is executed.

One of the interesting thing with the action filters is, we can even change the parameters before passed to an action and that s what we are going to see in this article.


All the action filters should implement the IActionFilter interface.

public interface IActionFilter
    void OnActionExecuting(ActionExecutingContext filterContext);
    void OnActionExecuted(ActionExecutedContext filterContext);    	

The IActionFilter contains two methods. The OnActionExecuting method is called before the action is executed and the OnActionExecuted method is called after the action is executed.

In our example we are going to change the parameters before the action is called so we have to override the OnActionExecuting method.


Let say we have a controller named OrdersController with a single action Index that returns the list of orders.

public ActionResult Index(int[] ids)
  // Returns all the orders if the array is null else 
  // returns only the orders whose ids are in the array.

The Index action takes an integer array (ids) as the input parameter. If the user doesn't pass any id then all the orders will be returned from the action else only the orders of the passed ids will be returned. The important thing is multiple order ids are passed as comma separated values to the action for ex. http://localhost:5555/orders/1,2,3.

Without any customization if the user sends the above request then the value of parameter ids will be null because MVC not converts the comma separated values into an integer array and there comes our custom action filter for rescue. In the custom action filter we read the comma separated values, convert into an integer array and set it to the parameter.

So here we go!

public class ParameterConverterAttribute : FilterAttribute, IActionFilter
  private readonly string _parameterName;

  public ParameterConverter(string parameterName)
    _parameterName = parameterName;

  public void OnActionExecuting(ActionExecutingContext filterContext)
    var valueProviderResult = filterContext.Controller.ValueProvider.GetValue(_parameterName);

    if (valueProviderResult != null)
      var orders = valueProviderResult.AttemptedValue.ToString().Split(',');
      filterContext.ActionParameters[_parameterName] = Array.ConvertAll(orders, s => int.Parse(s));

  public void OnActionExecuted(ActionExecutedContext filterContext)

The implementation is quite simple! We are reading the parameter's actual value(AttemptedValue) from the ValueProvider and converting it into an integer array. The converted array is assigned to the corresponsing key in the ActionParameters collection. Since we are passing the parameter name  in the constructor we can reuse our custom filter in other controller actions as well. 

So we are done with the implementation and the only thing pending is applying it over the action. Let's do it.

public ActionResult Index(int[] ids)
	// Returns all the orders if the array is null else 
	// returns only the orders whose ids are in the array.

That's it! Now the comma separated IDs are available as an integer array in the action.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Trigent Software Private Limited
India India
I'm a software developer from south tip of India. I spent most of the time in learning new technologies. I've a keen interest in client-side technologies especially JavaScript and admire it is the most beautiful language ever seen.

I like sharing my knowledge and written some non-popular articles. I believe in quality and standards but blames myself for lagging them.

I believe in small things and they makes me happy!
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 5 Pin
Christian Amado29-Aug-12 10:55
memberChristian Amado29-Aug-12 10:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 29 Aug 2012
Article Copyright 2012 by After2050
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid