.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share

 Subscribe to Articles

Action Filter in ASP.Net MVC framework

Posted By :Dhananjay Kumar      Posted Date :10/12/2009   Points :25   Category: ASP.NET    URL: http://www.dhananjaykumar.net
 


Action Filter in ASP.Net MVC framework.

Objective

This article will give an introduction of Action Filter in ASP.Net MVC framework. This will also explain how to create a custom Action filter.

Action Filters in ASP.Net MVC framework

image1.gif

  1. Action filters are attribute
  2. This could be applied on a particular Action
  3. This could be applied on a Controller.
  4. This modifies the way Action executes.
  5. Custom Action filter could also be created and applied.

ASP.Net MVC framework Action filters could be classified as,

image2.gif

How to use an Action filter?
  1. Create a ASP.NET MVC application, by selection File->New->Project->Web
  2. Right click on Controller folder and add one controller. Feel free to give any name. Here name is TestController.
  3. Create an Action inside the controller. Feel free to give any name of the Action. Here name is TestingActionFilter()
  4. TestingActionFilter() action is returning current time as string.
  5. Apply Action filter OutputCache on the Action.
  6. Give required parameter for OutputCache action filter.

    image3.gif

    7. Run the application.
    8. Type http://localhost:1903/Test/TestingActionFilter to get the output. Port number may differ in your case.

    image4.gif

Types of Action filter

image5.gif 

image6.gif

Action filter class

  1. The base class of all Action filter are ActionFilterAttribute class.
  2. This is under namespace System.Web.Mvc
  3. It extends FilterAttribute Class.
  4. Class contains four methods , which is shown below

    image7.gif


Creating a Custom Action Filter

 image8.gif
  1. Add a class in MVC project. Feel free to give any name.


    image9.gif
     
  2. Extend the class ActionFilterAttribute
  3. Override the methods. Call method Message in all overridden function.

    image10.gif
     
  4. Custom Action filter class contains one method called Message. This method is simply writing name of the controller and action on Debug output.
  5. On Action of TestController apply this custom Action filter.

    image11.gif
     
  6. See the output in Debug mode

    image13.gif
Custom Action filter is writing name of the Controller and Action on Debug.

image14.gif

Conclusion

This article gave an introduction of Action Filter in ASP.Net MVC framework. This also explained how to create a custom Action filter


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend