Archive

Posts Tagged ‘Custom Filters’

Filters in MVC

July 18, 2015 Leave a comment

Using filters you can inject you business logic or business rules into the Action Methods or to entire controller. Business rules can be applied before and after the action method gets executed.

Some of your action filters 
   1) OutputCache

– It will catch the out put of action method up to certain time period.
CacheProfiler > Define the cacheProfiler in web.config  and call it in controller action method. So that when you change it in later you no need to redeploye the code.

   2) HandleError

– Handles the error if any error occurs while executing the action method.
– No need to add separately to each action method or Controller since HandleErrorAttribute is added globally in Global.asax page.
<customErrors mode=”On”/> in root web.config of root application.
– we can also create common Exception Controller and specify the error code and the controller to redirect under customErrors of web.config file.
   3) Authorize

  • Allow/Restrict  particular user(or)roles.
  • By default all action methods are accessible all the anonymous and authenticated users in Mvc.
  • To allow only authenticated to access the Action Method use [Authorize] attribute.

   4) ChildActionOnly

ChildActionMethod Invoked by making child request from view using ‘Action()’, ‘RenderAction()’ html helper.
– When the Action Method is decorated with [ChildActionOnly] we can’t access it through URL. If you try to access it will throw you ‘ChildActionMethod can be accessible only through the child request’
ChildActionOnly typically associated with partial view but this is not compulsory.
– [NonAction] different from [ChildActionOnly], [NonAction] attribute can not be called through URL or through ‘Action()’, ‘RenderAction()’ html helper.
– Can cache portion of view through ChildActionMethod.
5) RequireHttps

– Force the actionmethod to execute and display it  through ssl layer
– So that data that is transmitted are secure.

you can create your own custom filters. In MVC 4.0 you have 4 types of custom filters

  1. Authorization Filter – Implements IAuthorizationFilter interface.
  2. Action Filter – Implements the IActionFilter interface.
  3. Result Filter – Implements the IResultFilter interface.
  4. Exception Filter – Implements the IExceptionFilter interface.

Filters will be executed in above order. If you want to control the order in which filters of same type are executed using filter’s Order property. System.Web.Mvc.FilterAttribute  is the base class for all the Filter actions, when ever you implement custom filter attribute you should inherit this base class and any one or more above mention filters.
ActionFilterAttribute this base class inherits IAction and IResult interfaces

The base ActionFilterAttribute class has the following methods that you can override:
OnActionExecuting – This method is called before a controller action is executed.
• OnActionExecuted – This method is called after a controller action is executed.
OnResultExecuting – This method is called before a controller action result is executed.
OnResultExecuted – This method is called after a controller action result is executed.

For More Refer here (Click Here)

Advertisements
%d bloggers like this: