Article Overview
- Background
- Implementation of Action Filter (OnActionExecuting)
- Inject Filter at a different level
- Skip Filter at a different level
- Pre-requisites
- How to block an IP address using Action Filter
- Implementation of Action Filter (OnActionExecuting)
- Inject Filter at a different level
- Skip Filter at a different level
- Complete example
- Implementation of Action Filter (OnActionExecuting)
- Inject Filter at a different level
- Skip Filter at a different level
- Output
- Summary
Background
There was a requirement for an MVC web application where I had to allow application access only to specific IP addresses. In other words, I had to block IP addresses that weren't valid. I have gone through various sites to implement Filters, Action Filters, Get IP address, etc. Hence, I had to customize searched solutions in an effective and easy way.
This article mainly focuses on three key things:
- Implementation of Action Filter (OnActionExecuting)
- Inject Filter at a different level
- Skip Filter at a different level
Here, I have kept all the implementation details along with a complete example.
Prerequisites
You MUST have basic knowledge of ASP .NET MVC, Filters, and Action Filter.
How to block an IP address using Action Filter
There are mainly three key steps to implement this:
- Implementation of Action Filter (OnActionExecuting)
- Inject Filter at a different level
- Skip Filter at a different level
Now, let us see in detail.
Implementation of Action Filter (OnActionExecuting)
First, add a folder “Attribute” into MVC Web Application.
Second, add a class “LogAttribute” into “Attribute” folder.
Third, inherit “ActionFilterAttribute” to “LogAttribute” class.
public class LogAttribute : ActionFilterAttribute
ActionFilterAttribute is an abstract class having four virtual methods (OnActionExecuted, OnActionExecuting, OnResultExecuted, and OnResultExecuting) which we can override in “LogAttribute” class.
Fourth, override “OnActionExecuting” method in “LogAttribute” class.
public override void OnActionExecuting(ActionExecutingContext filterContext)
Inject Filter at a different level
There are mainly three levels where you can inject a filter: Action, Controller, and Globally.
Action: When a filter is injected with a set of actions, only those actions will be injected
[Log]
public ActionResult About()
{
Controller: When a filter is injected into a controller class, all its actions are also injected
[Log]
public class HomeController : Controller
{
Globally: By registering filters globally will be triggered by all the actions performed in the application
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new LogAttribute());
}
Skip Filter at a different level
There are mainly two levels where you can skip an injected filter.
Globally filters can be skipped at Controller or Action level.
Filter injected at Controller can be skipped at the Action level.
First, create another filter SkipAttribute and inherit System.Attribute.
public class SkipAttribute : System.Attribute
Second, inject above filter at the action.
[Skip]
public ActionResult Error()
{
Third, modify LogAttribute to ignore for SkipFilter.
bool skipFilter = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipAttribute), true) || filterContext.ActionDescriptor.IsDefined(typeof(SkipAttribute), true);
Here, I have given high-level steps for our requirement. Now, let us see the complete example which will give you more clarity about these steps.
Complete example
For the complete example, I have prepared and uploaded a file which contains all the code.
- Implementation of Action Filter (OnActionExecuting)
- Inject Filter at different level
- Skip Filter at different level
- Output
Implementation of Action Filter (OnActionExecuting)
Inject Filter at a different level
Skip Filter at a different level
Output
First, valid IP.
Second, invalid IP.
Third, Log tracking file.
Summary
Now, I believe you will be able to implement Action Filter (OnActionExecuting) along with injecting at different levels as well as skipping at a specific level.