The use of filters

As we mentioned previously, filters can have three different scopes: global, controller, and action. In the first case, the filter is applied globally in the Startup class. In the other two cases, the filter is used in the form of an attribute and is usually applied to the controller class definition or the action method definition. Let's take a closer look at these different approaches. The application of a filter in the Startup class means that the filter covers all the routes in the MVC pipeline, as follows:

    public class Startup
{
...

public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers(config => config.Filters.Add(new
CustomFilter()
));
}

...
}

In this case, CustomFilter has a global scope. The config.Filters attribute is a collection of IFilterMetadata interfaces. This interface is used to describe the filters that are present in our MVC pipeline. It should be noted that the collection doesn't check duplicates, which means that we may potentially add two filters of the same type.

Since FilterCollection doesn't consider duplicates, it is possible that, in a large codebase, a filter type is accidentally initialized more than once, which may affect the performance of our service. It is vital to pay attention to code merges, especially in a distributed team. By using pull requests and holding code review meetings, this kind of silent issue can be avoided.

On the other hand, the controller scope and the action scope are restricted to a specific controller or action. The best way to use a filter on a particular controller or action is by extending a filter attribute. ASP.NET Core provides some built-in filter attributes. For each filter type, the framework provides a corresponding class that provides methods for overriding. For example, this is the case for the ActionFilterAttribute type:

using Microsoft.AspNetCore.Mvc.Filters;

namespace SampleAPI.Filters
{
public class CustomControllerFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext
context)
{
// do something before the action executes
}

public override void OnActionExecuted(ActionExecutedContext
context)
{
// do something after the action executes
}
}
}

The  CustomControllerFilter extends the ActionFilterAttribute type, which contains the OnActionExecuting and OnActionExecuted methods. The filter can be applied to a specific controller class or action method using the syntax of the attributes:

...
[Route("api/order")]
[CustomControllerFilter]
public class OrderController : ControllerBase
{

...

Under the hood, ActionFilterAttribute is an abstract class and implements the IActionFilter interface type, which we looked at previously. Therefore, by taking a quick look at the ActionFilterAttribute class, we can assume that this abstract class also provides the IAsyncActionFilter, IResultFilter, and IAsyncResultFilter methods:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public abstract class ActionFilterAttribute :
Attribute, IActionFilter, IFilterMetadata, IAsyncActionFilter, IResultFilter, IAsyncResultFilter, IOrderedFilter
{
...
}

The preceding code snippet describes how ASP.NET Core always provides an easy way for us to customize and extend the behavior of our frameworks. Furthermore, the user has full control over the code and the interfaces provided by the framework. Eventually, we can easily create a custom abstract type that implements a custom behavior for the filter and can be extended by other concrete filter classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.53.209