1.

What are Filters in MVC

Answer»

When we want to be executed just prior to the action execution, when the USER sends the request to the controller, in this the user create the custom class which works as a filter where the developer writes the logic which will be implemented just prior to the action execution, By programming method we can apply filters to the actions ,There are different type of filter use in MVC :

  1. Authorization: Authorization and authentication before executing the actions of the controller. built-in filter used is [Authorize], [RequireHttps], interface used is IAuthorizationFilter.
  2. Action Filters: it performs some action before and after the action methods execute before and after executing the action in the controller, there is no built-in filter used in this and interface used is IActionfilter.
  3. Result Filter: it basically PERFORMANCE some operation after and before the execution of VIEW result. The build is filter used is [OutputCache].the interface used is IResultFilter.
  4. Exception filters: Performs some operation if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline. The built used is [HandleError] and the interface used is IExceptionFilter.

When there is any kind of exception in an APPLICATION then the exception filter executes. HandleErrorAttribute ([HandlerError]) class is a built-in exception filter class in MVC framework. The HandleErrorAttribute class renders Error.cshtml which is inside the shared folder which is added by default during project creation when an unhandled exception OCCURS.

[HandleError]

public class HomeController : Controller { public ActionResult Index() { throw new Exception("This is unhandled exception"); return View(); } public ActionResult About() { return View(); } public ActionResult Contact() { return View(); } }


Discussion

No Comment Found