Updated April 1, 2023
Introduction to ASP.NET Core Filter
ASP.NET Core Filter mainly enables the code to be executed before or after precise stages in the request of the pipeline process. There will be several built-in filters: Caching, Authorization and Exception Handling, Logging, and so on. The ASP.NET Core filters which help to avoid Code-Duplication across the action methods. To return the Cached response short-circuiting the pipeline request.
ASP.NET Core Filter Overviews
ASP.NET Core Filters allow us to execute the custom coding before or after the execution of the action method. In addition, the Filters make available with several ways to do general recurring tasks on action methods. ASP.NET Core Filters are raised on several stages based on the request pipeline process.
There are several built-in Filters accessible with Core MVC. We can also build custom filters; it handles tasks like Authorization, which secures access with resources like if the user is not authorized for. It also facilitates eliminating the duplication codes in the application of the action method.
How filters work ASP.NET
Filters execute with the ASP.NET Core action pipeline invocation, which is mentioned because of the pipeline filter. The ASP.NET Core Filter Pipeline executes while the ASP.NET Core chooses the action to execute. When the filter runs with the pipeline, there will always be various scenarios for each execution. So when developing a filter before analyzing the necessities, it’s easier to decide which filters we need accurately and in which pipeline filter position for the executions.
<image>
Working With Filters In ASP.NET
The Filters will usually execute from the Action method of MVC, which is known as the Pipeline Filter, and it runs only when the Action Method executes. The various filter level types execute entirely at various points along the pipeline. In this Pipeline Filter, a few filters run before the execution of the subsequent level, which is like an Authorization Filter. In addition, their filters are executed after and before the execution in the Pipeline Filter; for example, Action Filter is the one type.
ASP.NET Core Filter Run Code
To build an Action Filter, we must build the class that inherits from the IActionFilter interface or IAsyncActionFilter interface. The IActionFilter and IAsyncActionFilter implemented the class ActionFilterAttribute; let’s see the line of code below,
Public abstract class ActionFilterAttribute: Attribute, IActionFilter, IFilterMetadata, IAsyncActionFilter, IResultFilter, IAsyncResultFilter, IOrderedFilter
Here in the example, we inherited the IActionFilter interface because it contains the definition methods that we need. To implement the synchronous Action Filter, which executes the after and before action method execution, we must implement the OnActionExecuted and OnActionExecuting methods,
namespace ActionFilters.Filters
{
public class ActionFilterExample : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
//this method code is for the before execution
}
public void OnActionExecuted(ActionExecutedContext context)
{
// this method code is for the after execution
}
}
}
ASP.NET Core Filter Types
It enables the code which is to be executed before or after precise stages in the request of the pipeline process. In ASP.NET Core filters the entire execution based only on the MVC Action Pipeline; there will also be the custom filters in ASP.NET Core. The Pipeline Filters runs while the action of the MVC Controller requires executing, and the particular execution may do already. There will be various types of Filters available in the ASP.NET Core. Let’s see the various types with their important procedures,
- Authorization Filters
This Filter helped out to work out whether the user allowed for the particular present request. However, once the particular user is not authorized for the specific request, the filter finally breaks the flow of the pipeline process. Here we have to develop an extra additional custom authorization filter.
- Resource Filters
This Filter is used to function to handle the authorized request in the process flow. The resource filter executes on after and before the flow of execution in the process filter. In general, the filter type activates before the data model binding in the Controller Level. For the Caching Implementation purpose, this filter is used. The interfaces IAsyncResourceFilter or IResourceFilter are implemented by this resource filter.
- Action Filters
The Action Filters is required to be used when we require executing the filter code immediately with the controller action method. The usage of the action filter is on after or before the execution of the controller action method. For example, we need to add manipulation of arguments agreed into the Nursing Action of Associate. The interfaces IAsyncActionFilter or IActionFilter are implemented by Action Filters.
- Exception Filters
To track the exception type at any time of code execution and then return the exception message for the request process raised, we are required to use the Exception Filters. The interfaces IEceptionFilter or IAsyncExceptionFilter are implemented by the Exception Filters. Basically, the Exception Filter is used to handle logging in any application or to handle familiar error trapping messages.
- Result Filters
We need to use the Result Filter if we require tracking the result of any controller action method. Therefore, the interfaces IResultFilter or IAsyncResultFilter are implemented by these Result Filters.
ASP.NET Core Filter Attributes
The ASP.NET Core Attributes are of several types; let’s seeing the following attributes,
- ServiceFilterAttribute
- TypeFilterAttribute
- IFilterFactory Implemented on Attribute
ServiceFilterAttribute
These attributes get back the instance of the filter by using the (DI) Depending Injection. We required including this filter to the container to reference it in ServiceFilter Attribute in Controller class or Action Method and in ConfigureService.
IFilterFactory
IFilterFactory is implemented by the ServiceFilterAttribute, which depicts the method for developing the IFilter Instance. The method CreateInstance is used to load the particular type of DI (Depending Injection) from the Service Container.
TypeFilterAttribute
This attribute is related to the ServiceFilterAttribute, and the interface IFilterFactory is implemented by this attribute. The types are referenced in TypeFilterAttribute required to register in the ConfigureService method. This attribute be accepted in constructor arguments for the type.
Conclusion
This article has explained the Filters in ASP.NET Core, which enables the execution of the code after or before a certain period in the request pipeline process. I hope the article helps to understand.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET Core Filter” was beneficial to you. You can view EDUCBA’s recommended articles for more information.