8
Reply

What are action filters in asp.net MVC ?

Sravan N

Sravan N

7y
3.3k
1
Reply

    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.

    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();} }

    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.

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

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

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

    Authorization Filter Action Filter Result Filter Exception Filter

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