Controllers as action filters in ASP.NET Core MVC

It is common to leverage action filters when building MVC applications – this was the case in classic ASP.NET MVC, in ASP.NET Web API and is a still widely used technique (with much richer support!) in ASP.NET Core MVC.

What is not commonly known though, is that it’s possible for controllers to act as their own filters – so let’s have a look at this feature today.

Background

You can do a lot of useful things with filters, especially around validation of requests. One fairly typical example of filter usage would be to check if a given ID exists in the underlying data store.

In order to illustrate this fairly common usage scenario, let’s imagine the following controller (simplified for brevity, with only Get By ID and Delete actions):

In both Get By ID and Delete actions, we first check whether the Contact exists, and if it doesn’t, we issue a 404. Instead of checking in two (or more) places for whether a given ID is present in the data store, you can delegate this operation to a filter.

In ASP.NET Core MVC, filter can also have its own dependencies (such as the data storage) injected via constructor. However, for this to work, it has to be specified as TypeFilter. This is shown next.

We can then use such a filter to eliminate the duplicate code from our controller, as it would implicitly check for id in the action arguments, match this to the data storage and issue a 404 in case the entry is not there.

The simplified code looks like this:

However it is not necessarily a great idea to have the filter separate from the controller, cause, after all, they are tightly coupled to each other and would normally always be used together.

Therefore, a nice alternative is to use the controller as a filter for itself, and this is what this post is about.

Controller as action filter

Action filters are defined by IActionFilter and IAsyncActionFilter interfaces, and typically implemented directly or using the base class ActionFilterAttribute, which you can inherit from and override the necessary method. This is something that we have already seen in the previous snippets, with our ContactExistsFilter.

However, what is very interesting, is that the base Controller class is already a filter too – it implements both IActionFilter and IAsyncActionFilter interfaces, in both cases as no-op, virtual methods. So you can override them, and have your controller become a filter for itself.

This means that we can combine our ContactsController and ContactExistsAttribute into one class.

Since in this case both the controller and its “filter” functionality are the same class, they obviously have a single constructor for both use cases – so dependencies are universally available. In fact, the instance of the controller is only created once per request – both to act as filter in the filter pipeline and then as controller during the request handling. This also simplifies sharing state between the controller and action functionalities – should you need to do so. In the case of the earlier filter-controller separation two separate object instances were used per each request.

If you are referencing Microsoft.AspNetCore.Mvc.Core instead of Microsoft.AspNetCore.Mvc package – perhaps you are building lightweight APIs and want to omit things like Razor Views – then you will not have access to the base Controller class, and instead you’d likely be using ControllerBase instead, which is not acting as filter by default.

However, you can still make the controller act as filter by simply manually implementing one of the action filter interfaces – i.e. IAsyncActionFilter. In that case you should remember to mark the method implementation as [NonAction], otherwise the framework will attempt to use it as an action.

This is shown in the next snippet.

And that’s all! As usually, the source code for this article is available on Github.

Share the post!