

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the levels at which filters can be applied in ASP .Net MVC C#?
In an ASP .Net MVC application filters can be applied in three levels.
- Action Method Level
- Controller Level
- Global Level
Action Method Level
Filters that are applied at the Action Method level will work only particularly for that action method.
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ [Authorize] //Action Method Level public string Index(){ return "Index Invoked"; } } }
Controller Level
Controller level filters are applied to all the action methods. The following filter are applicable to all the action methods of the HomeController, but not on other controllers.
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ [Authorize] //Controller Level public class HomeController : Controller{ public string Index1(){ return "Index1 Invoked"; } public string Index2(){ return "Index2 Invoked"; } } }
Global Level
Global level filters are provided in the Application_Start event of the global.asax.cs file by using default FilterConfig.RegisterGlobalFilters() method. The global filters will be applied to all the controller and action methods of an application.
public class MvcApplication : System.Web.HttpApplication{ protected void Application_Start(){ AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } public class FilterConfig{ public static void RegisterGlobalFilters(GlobalFilterCollection filters){ filters.Add(new HandleErrorAttribute()); filters.Add(new AuthorizeAttribute()); } }
- Related Questions & Answers
- What is ViewData in ASP .Net MVC C#?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- Which of the following areas data mining technology can be applied?
- What are filters in JSP?
- What are the common driver methods that can be applied to browsers in Selenium with python?
- What are the document properties which can be accessed using Legacy DOM?
- What are intent-filters in Android?
- What are the curries in which cinnamon can be used to improve the taste?
- What are the ways in which black money can be converted into white money?
- Can Selenium be used for .NET applications?
- What are the different wildcard characters which can be used with NOT LIKE operator?
Advertisements