Adding Custom Action Filters

In ASP.NET Core, filters allow the running of code before or after the execution of a particular resource in the pipeline. It can be configured on method, controller, or globally. They run within the MVC Action Invocation pipeline known as Filter pipeline when a particular action is selected by MVC based on the routing:

ASP.NET Core provides various filters, such as Authorization filters to decide whether the user accessing the resource is authorized or not. Resource filters are used to filter incoming requests in the pipeline and are mostly used for caching. Exception filters are used to apply policies that run globally to handle unhandled exceptions. Action filters help wrap calls to individual action methods and Result filters to wrap the execution of independent action results.

An Action filter is an attribute that can be applied on controller and action method levels.

In this chapter, we will develop these two action filters:

  • LoggingActionFilter
  • TransactionActionFilter

Logging Action Filter

We will create a custom LoggingActionFilter filter to log an exception when any method is executed. This way, we can log the information for every controller which has this attribute annotated:

    public class LoggingActionFilter : ActionFilterAttribute 
{
public override void OnActionExecuting(
ActionExecutingContext context)
{
Log("OnActionExecuting", context.RouteData,
context.Controller);
}

public override void OnActionExecuted(
ActionExecutedContext context)
{
Log("OnActionExecuted", context.RouteData,
context.Controller);
}

public override void OnResultExecuted(
ResultExecutedContext context)
{
Log("OnResultExecuted", context.RouteData,
context.Controller);
}

public override void OnResultExecuting(
ResultExecutingContext context)
{
Log("OnResultExecuting", context.RouteData,
context.Controller);
}

private void Log(string methodName, RouteData routeData,
Object controller)
{
var controllerName = routeData.Values["controller"];
var actionName = routeData.Values["action"];
var message = String.Format("{0} controller:{1}
action:{2}", methodName, controllerName, actionName);
BaseController baseController =
((BaseController)controller);
baseController.Logger.LogInformation(
LoggingEvents.ACCESS_METHOD, message);
}
}

In the preceding code, the Log method is the helper method, which takes the method name, RouteData and Controller, and logs the information in the Logger instance injected through DI.

Transaction Action Filter

To handle transactions, we will develop a custom TransactionActionFilter, which can be used to begin and end transactions and to commit or rollback in case any error occur.

Here is the code for TransactionActionFilter:

    public class TransactionActionFilter : ActionFilterAttribute 
{
IDbContextTransaction transaction;
public override void OnActionExecuting(
ActionExecutingContext context)
{
((BaseController)context.Controller).ActionManager
.UnitOfWork.BeginTransaction();
}

public override void OnActionExecuted(
ActionExecutedContext context)
{
if (context.Exception != null)
{
((BaseController)context.Controller).ActionManager
.UnitOfWork.RollbackTransaction();
}
else
{
((BaseController)context.Controller).ActionManager
.UnitOfWork.CommitTransaction();
}
}
}
..................Content has been hidden....................

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