MVC Filters  Eyal Vardi  CEO E4D Solutions LTD  Microsoft MVP Visual C#  blog: www.eVardi.com
Agenda           What is Filters?           Built-in Filters           Filter Interfaces           Custom Filters    ...
ASP.NET MVC Filters           Filters are custom classes that provide both a            declarative and programmatic mean...
Filter Interfaces                                           Action Method                   Action Result                 ...
Controller Context                        1                                                            6                  ...
Filter Order           Filters run in the following order:                  Authorization filters                  Acti...
Controller & Filters           The Controller class implements each of the            filter interfaces.           You c...
“Install” Filters           You can “Install” a filter in the following ways:                  Attribute on Actions or C...
IAuthorizationFilter           Make security decisions about whether to            execute an action method.            ...
Authorize Attribute Sample           Authorization won’t be granted unless both            conditions are met, Users & Ro...
Custom Authorization Policy          public class MyAuthorizationAttribute : AuthorizeAttribute          {             pro...
IActionFilter Interface           OnActionExecuting                  Runs before the action method.           OnActionE...
IActionFilter Context’s© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IResultFilter Interface             OnResultExecuting                  Runs before the ActionResult object is executed. ...
IResultFilter Context’s© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IExceptionFilter           Execute if there is an unhandled exception            thrown during the execution of the ASP.N...
Handle Error Attribute           You can specify an exception and the names            of a view and layout.           W...
Custom Exception Filter        public class MyExceptionAttribute: FilterAttribute, IExceptionFilter        {             p...
Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
First                    Global                    Controller                 Action           Last   Filter I            ...
Custom Filter           You can create a filter in the following ways:                  Override one or more of the cont...
Custom Attribute Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Global Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filter Providers           By default, ASP.NET MVC registers the            following filter providers:                 ...
The Filter Provider Interface           The GetFilters method returns all of the            IFilterProvider instances in ...
Upcoming SlideShare
Loading in …5
×

Asp.net mvc filters

1,408
-1

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,408
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
112
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Asp.net mvc filters

  1. 1. MVC Filters Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  2. 2. Agenda  What is Filters?  Built-in Filters  Filter Interfaces  Custom Filters  Filter Providers© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  3. 3. ASP.NET MVC Filters  Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods. [HandleError] [Authorize] public class CourseController : Controller { [OutputCache] [RequireHttps] public ActionResult Net( string name ) { ViewBag.Course = BL.GetCourse(name); return View(); } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  4. 4. Filter Interfaces Action Method Action Result 1 2 3 4© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  5. 5. Controller Context 1 6 2 3 4 5© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  6. 6. Filter Order  Filters run in the following order:  Authorization filters  Action filters  Response filters  Exception filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  7. 7. Controller & Filters  The Controller class implements each of the filter interfaces.  You can implement any of the filters for a specific controller by overriding the controllers On<Filter> method.  OnAuthorization  OnActionExecuting  OnActionExecuted  OnResultExecuting  OnResultExecuted  OnException© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  8. 8. “Install” Filters  You can “Install” a filter in the following ways:  Attribute on Actions or Controllers  Add to Global Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  9. 9. IAuthorizationFilter  Make security decisions about whether to execute an action method.  AuthorizeAttribute  RequireHttpsAttribute© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  10. 10. Authorize Attribute Sample  Authorization won’t be granted unless both conditions are met, Users & Roles. [Authorize(Users=“Eyal, Oz”, Roles=“Admin”)] public class CourseController : Controller { public ActionResult Net( string name ) { ViewBag.Course = BL.GetCourse(name); return View(); } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  11. 11. Custom Authorization Policy public class MyAuthorizationAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { return httpContext.Request.IsLocal || AuthorizeCore(httpContext); } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  12. 12. IActionFilter Interface  OnActionExecuting  Runs before the action method.  OnActionExecuted  Runs after the action method© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  13. 13. IActionFilter Context’s© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  14. 14. IResultFilter Interface  OnResultExecuting  Runs before the ActionResult object is executed.  OnResultExecuted  Runs after the result.  Can perform additional processing of the result.  The OutputCacheAttribute is one example of a result filter.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  15. 15. IResultFilter Context’s© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  16. 16. IExceptionFilter  Execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline.  Can be used for logging or displaying an error page.  HandleErrorAttribute is one example of an exception filter.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  17. 17. Handle Error Attribute  You can specify an exception and the names of a view and layout.  Works only when custom errors are enabled in the Web.config file  <customErrors mode="On" /> inside the <system.web>  The view get HandleErrorInfo© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  18. 18. Custom Exception Filter public class MyExceptionAttribute: FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (!filterContext.ExceptionHandled && filterContext.Exception is NullReferenceException) { filterContext.Result = new RedirectResult("/MyError.html"); filterContext.ExceptionHandled = true; } } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  19. 19. Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  20. 20. First Global Controller Action Last Filter I Filter I Filter I Filter I Filter I Filter II Filter II Filter II Filter II Filter II Action Method Action Result Authorization Action Result Exception Filter I Filter I Filter I Filter I Filter I Filter II Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter II Filter II Filter I Filter I Filter I Filter I Filter II Filter I Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter I Filter I© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  21. 21. Custom Filter  You can create a filter in the following ways:  Override one or more of the controllers On<Filter> methods.  Create an attribute class that derives from ActionFilterAttribute or FilterAttribute.  Register a filter with the filter provider (the FilterProviders class).  Register a global filter using the GlobalFilterCollection class.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  22. 22. Custom Attribute Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  23. 23. Custom Global Filters© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  24. 24. Filter Providers  By default, ASP.NET MVC registers the following filter providers:  Filters for global filters.  FilterAttributeFilterProvider for filter attributes.  ControllerInstanceFilterProvider for controller instances.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  25. 25. The Filter Provider Interface  The GetFilters method returns all of the IFilterProvider instances in the service locator.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×