Action Filters in ASP.Net MVC framework

No.of Views972
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : ASP.NET , How to
Action Filters in ASP.Net MVC framework
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 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.

Image Loading....




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.


Image Loading...

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.



{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace usingActionFilter.Controllers
{
public class TestController : Controller
{

[OutputCache(Duration = 5,VaryByParam="none")]
public string TestingActionFilter()
{
return DateTime.Now.ToString();
}

}
}


{/codecitation}

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

Image Loading....

Image Loading....




Types of Action filter

Image Loading...

Image Loading....

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



{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;

namespace System.Web.Mvc
{

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
{
protected ActionFilterAttribute();


public virtual void OnActionExecuted(ActionExecutedContext filterContext);

public virtual void OnActionExecuting(ActionExecutingContext filterContext);

public virtual void OnResultExecuted(ResultExecutedContext filterContext);

public virtual void OnResultExecuting(ResultExecutingContext filterContext);
}
}


{/codecitation}

Image Loading...

Creating a Custom Action Filter

Image Loading....

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

Image Loading...

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



{codecitation class="brush: csharp; gutter: true;" width="700px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Diagnostics;

namespace usingActionFilter
{
public class CustomActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//base.OnActionExecuted(filterContext);

Message(filterContext.RouteData);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// base.OnActionExecuting(filterContext);
Message(filterContext.RouteData);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//base.OnResultExecuted(filterContext);
Message(filterContext.RouteData);
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
// base.OnResultExecuting(filterContext);

Message(filterContext.RouteData);
}
public void Message(RouteData route)
{
var _controllerName = route.Values["controller"];
var _methodName = route.Values["action"];
var _returnMessage = String.Format("controller = {0},action {1}", _controllerName, _methodName);

Debug.WriteLine(_returnMessage);
}


}
}

{/codecitation}

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.


{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using usingActionFilter;

namespace usingActionFilter.Controllers
{
public class TestController : Controller
{

[CustomActionFilter]
public string TestingActionFilter()
{
return "D";
}

}
}

{/codecitation}

6. See the output in Debug mode

Image Loading....

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

Image Loading...


For Better understanding of, how custom Action filter is working, put breakpoint and run the application.

Conclusion

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

Thank you


About the Author


Dhananjay Kumar
Description :I done my engineering from Anand Engineering college Agra in 2007. I am MCTS WCF, MCTS MOSS Development, I am MCTS Web Development . I am native of Jamshedpur. Currently Please feel free to contact me regarding any clarification of my article at Dhananjay.25july@gmail.com

Occupation : Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dhananjay25

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top