在 ASP .Net MVC 应用程序中,过滤器可以应用于三个级别。
在操作方法级别应用的过滤器仅适用于该级别 动作方法。
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ [Authorize] //Action Method Level public string Index(){ return "Index Invoked"; } } }
控制器级别过滤器应用于所有操作方法。以下过滤器是 适用于 HomeController 的所有操作方法,但不适用于其他操作方法 控制器。
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.asax.cs的Application_Start事件中提供 使用默认的 FilterConfig.RegisterGlobalFilters() 方法创建文件。全局过滤器 将应用于应用程序的所有控制器和操作方法。
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()); } }
以上就是在 ASP .Net MVC C# 中可以应用过滤器的级别是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号