explain details about this topic
Loading
explain details about this topic
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VelladuraiRank #739 · 3.2K reputationPosted Jun 17, 2024, 6:35 AM EDT
can you please reply with example?
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Jun 17, 2024, 6:43 AM EDT
https://www.c-sharpcorner.com/article/filters-in-Asp-Net-mvc-5-0-part-twelve/
https://www.c-sharpcorner.com/article/overview-on-types-of-filters-in-asp-net-mvc-5/
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 17, 2024, 6:51 AM EDT
Here is the example,
Authorization Filter
public void OnAuthorization(AuthorizationContext filterContext) { if (filterContext.HttpContext.Request.IsAuthenticated) { if (!Roles.IsUserInRole("Admin")) { ViewResult result = new ViewResult(); result.ViewName = "Error"; result.ViewBag.ErrorMessage = "Invalid User"; filterContext.Result = result; } } }Action Filter
public class CustomActionfilter : FilterAttribute, IActionFilter { public void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.HttpContext.Session["UserID"] != null) { filterContext.Result = new RedirectResult("/Home/Index"); } else { filterContext.Result = new RedirectResult("/Login /Login"); } } public void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Session["UserID"] != null) { filterContext.Result = new RedirectResult("/Home/ Index"); } else { filterContext.Result = new RedirectResult("/Login /Login"); } } } [CustomActionfilter] [AcceptVerbs(HttpVerbs.Get)] public ActionResult GetPerson() { Person p = new Person(); return View("Person", p); }Result Filter
public class CustomResultfilter : FilterAttribute, IResultFilter { public void OnResultExecuted(ResultExecutedContext filterContext) { if (filterContext.HttpContext.Session["UserID"] != null) { filterContext.Result = new RedirectResult("/Home/Contact"); } else { filterContext.Result = new RedirectResult("/Login/Login"); } } public void OnResultExecuting(ResultExecutingContext filterContext) { if (filterContext.HttpContext.Session["UserID"] != null) { filterContext.Result = new RedirectResult("/Home/Contact"); } else { filterContext.Result = new RedirectResult("/Login/Login"); } } } [CustomResultfilter] [AcceptVerbs(HttpVerbs.Get)] public ActionResult GetPerson() { Person p = new Person(); return View("Person", p); }Exception Filter
public class CustomExceptionFilter : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { filterContext.Controller.ViewBag.onExceptionError = "ExceptionFilter filter called"; filterContext.HttpContext.Response.Write("ExceptionFilter filter called"); } } [CUstomExceptionFilter] public class HomeController : Controller { }Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 17, 2024, 6:34 AM EDT
Hello,
In ASP.NET MVC, a user request is routed to the appropriate controller and action method. However, there may be circumstances where you want to execute some logic before or after an action method executes. ASP.NET MVC provides filters for this purpose.
ASP.NET MVC Filter is a custom class where you can write custom logic to execute before or after an action method executes. Filters can be applied to an action method or controller in a declarative or programmatic way. Declarative means by applying a filter attribute to an action method or controller class and programmatic means by implementing a corresponding interface.
MVC provides different types of filters. The following table list filter types, built-in filters, and interface that must be implemented to create custom filters.
Thanks
Vishal YelveRank #178 · 13.6K reputationPosted Jun 18, 2024, 1:57 AM EDT
Hi,
Do refer below article
https://www.c-sharpcorner.com/article/overview-on-types-of-filters-in-asp-net-mvc-5/
Ankit Rajput41 reputationPosted Jun 18, 2024, 2:00 AM EDT
Filters in ASP.NET MVC are attributes that allow you to add pre and post-processing logic to controller action methods. They provide a way to execute code before or after specific stages in the request processing pipeline. Filters help in addressing cross-cutting concerns such as logging, authorization, and error handling without cluttering the action methods with repetitive code.
Types of Filters in ASP.NET MVC:
Authorization Filters:
[Authorize]Action Filters:
[HandleError]Result Filters:
Exception Filters:
[HandleError]Example:
Here’s a simple example of a custom action filter in ASP.NET MVC:
public class CustomActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Code that runs before the action method executes var request = filterContext.HttpContext.Request; filterContext.HttpContext.Trace.Write("CustomActionFilter", "OnActionExecuting called"); base.OnActionExecuting(filterContext); } public override void OnActionExecuted(ActionExecutedContext filterContext) { // Code that runs after the action method executes filterContext.HttpContext.Trace.Write("CustomActionFilter", "OnActionExecuted called"); base.OnActionExecuted(filterContext); } }To use this custom action filter, you simply decorate your controller or action method with it:
[CustomActionFilter] public class HomeController : Controller { public ActionResult Index() { return View(); } }In this example, the
CustomActionFilterwill log trace messages before and after the execution of theIndexaction method in theHomeController.