Sravan N
What are action filters in asp.net MVC ?
By Sravan N in .NET on Feb 13 2017
  • Rajesh Kadam
    Mar, 2017 28

    Filtering in ASP.NET MVC Sometimes you want to perform logic either before an action method is called or after an action method runs. To support this, ASP.NET MVC provides filters. Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods.ASP.NET MVC Filter Types ASP.NET MVC supports the following types of action filters: 1. Authorization filters 2. Action filters 3. Result filters 4. Exception filters The filters are executed in the above given order.1. Authorization filters. These implement IAuthorizationFilter and make security decisions about whether to execute an action method, such as performing authentication or validating properties of the request. The AuthorizeAttribute class and the RequireHttpsAttribute class are examples of an authorization filter. Authorization filters run before any other filter. 2. Action filters. These implement IActionFilter and wrap the action method execution. The IActionFilter interface declares two methods: OnActionExecuting and OnActionExecuted. OnActionExecuting runs before the action method. OnActionExecuted runs after the action method and can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method. 3. Result filters. These implement IResultFilter and wrap execution of the ActionResult object. IResultFilter declares two methods: OnResultExecuting and OnResultExecuted. OnResultExecuting runs before the ActionResult object is executed. OnResultExecuted runs after the result and can perform additional processing of the result, such as modifying the HTTP response. The OutputCacheAttribute class is one example of a result filter. 4. Exception filters. These implement IExceptionFilter and execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline. Exception filters can be used for tasks such as logging or displaying an error page. The HandleErrorAttribute class is one example of an exception filter. NOTE : The filters can be applied at the action method, controller, or application level.

    • 5
  • Ayan Roy
    Mar, 2017 27

    filters are the type of event that happens before or after any types of action method event those define in controller. We have to add an attribute to the controller or action method.[HandleError] public class HomeController : Controller {public ActionResult Index(){ViewData["Message"] = "Welcome to ASP.NET MVC!";return View();}public ActionResult About(){return View();} }

    • 3
  • Vinod Kakade
    May, 2017 12

    IN ASP.NET MVC framework supports following action filters like1. Action Filters - Action filters are used to implement logic that gets executed before and after a controller action executes. We will look at Action Filters in detail in this chapter.2. Authorization Filters - Authorization filters are used to implement authentication and authorization for controller actions.3. Result Filters - Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.4. Exception Filters - Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.

    • 1
  • Solomon Yelcin
    Apr, 2019 24

    Filtering in ASP.NET MVC Sometimes you want to perform logic either before an action method is called or after an action method runs. To support this, ASP.NET MVC provides filters. Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods.ASP.NET MVC Filter Types ASP.NET MVC supports the following types of action filters: 1. Authorization filters 2. Action filters 3. Result filters 4. Exception filters The filters are executed in the above given order.1. Authorization filters. These implement IAuthorizationFilter and make security decisions about whether to execute an action method, such as performing authentication or validating properties of the request. The AuthorizeAttribute class and the RequireHttpsAttribute class are examples of an authorization filter. Authorization filters run before any other filter. 2. Action filters. These implement IActionFilter and wrap the action method execution. The IActionFilter interface declares two methods: OnActionExecuting and OnActionExecuted. OnActionExecuting runs before the action method. OnActionExecuted runs after the action method and can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method. 3. Result filters. These implement IResultFilter and wrap execution of the ActionResult object. IResultFilter declares two methods: OnResultExecuting and OnResultExecuted. OnResultExecuting runs before the ActionResult object is executed. OnResultExecuted runs after the result and can perform additional processing of the result, such as modifying the HTTP response. The OutputCacheAttribute class is one example of a result filter. 4. Exception filters. These implement IExceptionFilter and execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline. Exception filters can be used for tasks such as logging or displaying an error page. The HandleErrorAttribute class is one example of an exception filter. NOTE : The filters can be applied at the action method, controller, or application level.

    • 0
  • Rajireddy Kandi
    Oct, 2018 5

    Action Filters are additional attributes that can be applied to either a controller section or the entire controller to modify the way in which an action is executed.

    • 0
  • Parth Kale
    Oct, 2017 4

    Action filters are nothing but logical steps which you want to insert before or after and action method runs.

    • 0
  • manju Paul
    Sep, 2017 28

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

    • 0
  • manju Paul
    Sep, 2017 28

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

    • 0
  • Pankaj Sapkal
    Jun, 2017 26

    Authorization Filter Action Filter Result Filter Exception Filter

    • 0
  • Puneet Kankar
    Apr, 2017 7

    https://www.codeproject.com/Articles/577776/Filters-and-Attributes-in-ASPNET-MVC

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS