- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- What is ViewData in ASP .Net MVC C#?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- 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 different types of filters in C# ASP.NET WebAPI?
- What is biological magnification? Will the levels of this magnification be different at different levels of the ecosystem?
- What are intent-filters in Android?
- What are access specifiers in C#.NET?
- What are the different access specifiers in C#.NET?
- Can Selenium be used for .NET applications?

Advertisements