TechBubbles Microsoft Technology BLOG

Creating Action Filters in ASP.NET MVC Applications

Introduction

ASP.NET MVC Applications contains the action methods in the controllers. These actions are mapped through the routing when a user makes a browser request. In order to execute your logic before a action method is called or after an action method runs. ASP.NET MVC support this requirement through a concept called ActionFilters. 

ActionFilter Uses

    • Authentication and Authorization can be implemented using action filters.
    • User actions can be logged.
    • For setting up the Localization feature in the application.
    • For Changing the Application behavior based on browser user agent.

Creating an ActionFilter in ASP.NET MVC Application

You can write a ActionFilter class by inherting from ActionFileAttribute class. You can override the method OnActionExecuting and write the logic that executes before Action Method.

  1. To Add a ActionFilter to the ASP.NET MVC Application right click the Controllers folder in the project and select the add new item option and add the class template write the following code.

ActionFilter

 

2.   Applying Filtering attribute to the action method in the controller class

 

Action2

You can also apply the ActionFilter to all action methods in the controller.

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

1 Comment

By Kalyan Bandarupalli
TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud