Using Custom Action Filters in Class Libraries

21 Aug 2013 .Net, C#, Filters, MVC 2 Comments

I have recently been creating a class library that many MVC web applications are going to reference, and I wanted those applications to be able to implement some custom Action Filters so they did not need to duplicate code. I had originally thought this was not possible, due to the

Continue reading →

Passing in variables to custom Action Filters

20 Aug 2013 .Net, C#, Filters, MVC No Comments

Sometimes it is useful to have a reusable custom action filter, for example for checking user authorisation to access part of the application. I would prefer not to have a separate filter for each type of user, e.g. MustBeAdminAttribute, MustBeManagerAttribute, etc. There are several ways to pass variables to a

Continue reading →

Retrieving parameters from the query string in the URL in .Net

23 Jul 2013 .Net, C#, Filters, MVC No Comments

Here is a nice easy way to get the individual parameter values from the URL, returning either a NameValueCollection (System.Collections.Specialized.NameValueCollection HttpRequestBase.QueryString) of all parameters in the query string, or a string value of a particular named parameter.

var parameters = HttpContext.Request.QueryString;
var parameters = HttpContext.Current.Request.QueryString;
public class MyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var

Continue reading →