Extended Aspect-Oriented Programming (AOP) in .NET MVC
In the last post article, we discuss the Aspect-Oriented Programming(AOP) mechanism in the .NET MVC web application project(“Aspect-oriented programming in .NET MVC“). We start to talk about the extended aspect-oriented programming in the Data Access Layer(DAL) and the Business Logic Layer(BLL). It doesn’t like Microsoft.Extended.Logging easy to use. We need to design and create a few elements to use in web application projects. The new parts will help the web application project’s aspect-oriented programming to context the DAL and the BLL. This result will like Microsoft.Extended.Logging effective in the .NET Core web application project.
I hope this article can context the “Aspect-oriented programming in .NET MVC” article. I will create a new action filter file, the new controller files, and the view files for the extended aspect-oriented programming cases in my GitHub repository. You can compare two cases where the difference is and why it needs extended aspect-oriented programming in the .NET MVC web application project.
Extended Aspect-Oriented Programming Tutorial
Step 1. Create the new action filter class file under the Logging folder. The new Action-filter class file name sets “ExtenderdActionAttribute.”


Step 2. The ExtendedActionAttribute class must inherit the ActionFilterAttribute class and IActionFilter interface, which must add “using System.Web.Mvc.” The developer will need to write the four override methods, such as OnActionExecuted, OnActionExecuting, OnResultExecuted, and OnResultExecuting.

public class ExtendedActionAttribute : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
}
}
Step 3. We will need to know ActionExecutingContext’s ActionParameters information by the JsonCovert method. We are adding a new variable in the OnActionExecuting function area part.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string parametersInfo = JsonConvert.SerializeObject(filterContext.ActionParameters, new JsonSerializerSettings());
string routeData = JsonConvert.SerializeObject(filterContext.RouteData.Values);
Debug.WriteLine("OnActionExecuting");
Debug.WriteLine(routeData);
Debug.WriteLine(parametersInfo);
base.OnActionExecuting(filterContext);
}
Note: If you want to log information that can be easily readable, you will need to add the contract resolver parameter. The source code example will show below.
string parametersInfo = JsonConvert.SerializeObject(filterContext.ActionParameters, new JsonSerializerSettings()
{
ContractResolver = new ReadablePropertiesOnlyResolver()
});
class ReadablePropertiesOnlyResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (typeof(Stream).IsAssignableFrom(property.PropertyType))
{
property.Ignored = true;
}
return property;
}
}