ASP.Net MVC 5./ Filter Overrides

Filter Overrides in ASP.Net MVC 5.X

 

Filter Overrides in ASP.NET MVC 5 are very useful when we are implementing a global or controller level filter and we do not want to apply an action filter on some Action methods in the controller. This feature is useful for removing the headache of applying filters for each and every action where we need to exclude only a few actions. For following five types of filters available with MVC:

1.Authentication filters
2.Authorization filters
3.Action filters
4.Result filters
5.Exception filters

We have five type filter overrides corresponding to this:
– OverrideAuthenticationAttribute
– OverrideAuthorizationAttribute
– OverrideActionFiltersAttribute
– OverrideResultAttribute
– OverrideExceptionAttribute

We can mark any action method with an override filter attribute that essentially clears all filters in an upper scope (in other words controller level or global level).

Example: In the following example, the Authorize Filter is applied at the controller level.So, all the action methods of the Home controller can be accessed by the Admin user only. Now I want to exclude the Authorize Filter from the “About” method.

So in this case we can mark this “About” method with the “OverrideAuthorization” attribute. Now all the action methods of the home controller can be accessed by the Admin user except the “About” method. We can access the “About” action method without any authorization.

[Authorize(Users=”John”)]
     public class HomeController : Controller
              { public ActionResult Index() 
                                {
                                  return View(); 
                                        } 
                           [OverrideAuthorization]
                           public ActionResult About() 
                                              { 
                                                      return View(); 
                                                 }
                                                     } 

Note: The OverrideAuthorizationAttribute does not work properly with MVC version 5.0 due to some internal bug. This bug was resolved in MVC version 5.1 (preview).

Cancelling Filter Execution:

1. You can cancel filter execution in the OnActionExecuting methods by setting the Result property to a non-null value.

a. Any pending OnActionExecuted and OnActionExecuting filters will not be invoked
b.Invoker will not call the OnActionExecuted method for the canceled filter or for pending filters.
c.The OnActionExecuted filter for previously run filters will run.
d.All of the OnResultExecuting and OnResultExecuted filters will run.

2. You can cancel filter execution in the OnResultExecuting methods by setting the Cancel property true.
a. Any pending OnResultExecuted and OnResultExecuting are canceled and blank o/p is rendered to browser.
b. The OnResultExecuted filter for previously run filters will run.

 public class Log2Attribute : ActionFilterAttribute

                      {

                          public override void OnActionExecuting(ActionExecutingContext filterContext)

                               {

                                 //filterContext.Result = new EmptyResult();
                                 Trace(“On Action Executing”, filterContext.RouteData);

                            }

                  public override void OnActionExecuted(ActionExecutedContext filterContext)
                               {

                                Trace(“On Action Executed”, filterContext.RouteData);

                            } 
                 public override void OnResultExecuting(ResultExecutingContext filterContext)

                        {

                            filterContext.Cancel = true;
                                  //filterContext.HttpContext.Response.Write(“Advertisement <hr>”);
                                   Trace(“On Result Executing”, filterContext.RouteData);
                        }

             public override void OnResultExecuted(ResultExecutedContext filterContext)
                        {

                            //filterContext.HttpContext.Response.Write(“<hr>Advertisement”);
                             Trace(“On Result Executed”, filterContext.RouteData);

                      }
               public void Trace(string methodName, RouteData routeData)
                     {

                          string colName, actionName;

                          colName = routeData.Values[“controller”].ToString();
                             actionName = routeData.Values[“action”].ToString();
                         string str = string.Format(“2 – Method Name={0}, Controller Name={1},Action={2}”, methodName, colName, actionName);
                         System.Diagnostics.Trace.WriteLine(str);
                        HttpContext.Current.Response.Write(“<br>” + str + “<br>”);

                      } 

                 }

self paced learning on ASP.NET MVC 5.2

 

Leave a Reply