Action filter in MVC

75
Views
1
Answers

What is Action Filter and how it is implemented in c# MVC?

edit | flag
William Patrick
Asked on: Jun 16, 2011 at 5:08AM

1 Answers

16
2
16

To Log the users or the errors use Custom action Filters
create a custom action filter like this:

public class LogsRequestsAttribute : ActionFilterAttribute, IActionFilter
{
   
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
   
{
       
// Do some logging here to database or to file ...
   
}

   
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
   
{
       
// Do some logging here...
   
}

}

Add the Attribute to action method or to the controller itself depending on your need for logging

[LogRequest]
public class HomeController : Controller
{
   
...
}

[LogRequest]
public class AccountController : Controller
{
   
...
}

Thats it.. everytime the action with LogRequest attribute is executed it will hit action filter and will do youe logging.

edit | flag
Gokul A
Answered on: Jun 16, 2011 at 8:44PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags