0
Extending ASP.NET MVC<br />Part 1<br />Simone ChiarettaArchitect, Council of the EU<br />http://codeclimber.net.nz<br />Tw...
Who the hell am I?<br />Simone Chiaretta<br />Microsoft MVP ASP.NET<br />ASP Insider<br />Blogger – http://codeclimber.net...
Agenda<br />The default ASP.NET MVC Pipeline<br />Extensibilitypoints’ list<br />The Mostimportantextensibilitypoints<br />
The DefaultASP.NET MVC Pipeline<br />
The default ASP.NET MVC Pipeline<br />4<br />
The default components<br />URL Routing:<br />Parses the URL, andinstantiate the MvcHandler<br />Controller Factory<br />T...
ASP.NET MVC IS extensible<br />Ifyou don’t likethem<br />Ifyouneedsomething else<br />Changethem<br />
ASP.NET MVC IS extensible<br />Almosteveryaspectof the framework can beextented/replaced<br />
Extensibilitypoints’ list<br />
Routingextensibility<br />RouteConstraint<br />Validatesrouteparameterswith code<br />RouteHandler<br />Defineshow the req...
Controller Extensibility<br />ControllerFactory<br />ResponsibleforcreatingControllers<br />ActionInvoker<br />Invokesanac...
ActionFilterExtensibility<br />AuthorizationFilter<br />Makessure the currentrequestisallowed<br />ActionFilters<br />Exec...
Binding/ValidationExtensibility<br />ModelBinder<br />Populates the actionmethodparametersfrom the request<br />ModelValid...
ViewExtensibility<br />ViewEngine<br />The service thattransforms in HTML the data for the user<br />HtmlHelpers<br />Util...
The Mostimportantextensibilitypoints<br />
The MostimportantExtensibilitpoints<br />The onesyouwilldefinitelyuse<br />The onesyouhaveto start using<br />The onesyoum...
Extensibilitypointsyouwilluse<br />
CustomTemplates<br />
CustomTemplates<br /><ul><li>WHAT: Renders the html toedit/display specifictypes</li></ul>DEFAULT: Everythingis a label or...
CustomTemplates<br />AddPartialViews in:<br />/Views/Shared/DisplayTemplates<br />/Views/Shared/EditorTemplates<br />Demo<...
Server-sideValidationRules<br />
Server-sideValidationRules<br />WHAT: Definehow a propertyisvalidated in the server side validation<br />DEFAULT: Required...
Server-sideValidationRules<br />Write a newValidationAttribute<br />Implement the IsValidmethod<br />Applyattributetoyourm...
Client-sideValidationRules<br />
Client-sideValidationRules<br />WHAT: Definehow a propertyisvalidated in the client-side validation<br />DEFAULT: Client-s...
Client-sideValidationRules<br />First make a server-side validator<br />Makevalidationlogic in JavaScript<br />Writeadapte...
Extensibilitypointsyouhaveto start using<br />
Base Controller<br />
Base Controller<br />WHAT: The base class forevery Controller<br />DEFAULT: Defaultimplementation of helpermethods<br />WH...
Base Controller<br />Override Controller<br />YourcontrollersoverridefromBaseControllerinsteadof Controller<br />Demo<br />
HtmlHelpers<br />
HtmlHelpers<br />WHAT: Utility functionsthathideaway the generation of HTML markup or JavaScript code<br />DEFAULT: Html.T...
HtmlHelpers<br />Add new methodsasextensionmethods<br />Demo<br />
Rating<br />If you liked this talk, please consider rating it:<br />http://speakerrate.com/talks/3669-asp-net-mvc-extensib...
Extending ASP.NET MVC<br />Part 2<br />Simone ChiarettaArchitect, Council of the EU<br />http://codeclimber.net.nz<br />Tw...
Filters<br />
AuthorizationFilter<br />WHAT: Makes sure the current request is allowed<br />DEFAULT: Itisbased on the ASP.NETMembership ...
Action Filters<br />WHAT: Executed before or after the action executes<br />DEFAULT: Output cache<br />WHY: Addyourownbase...
ResultFilters<br />WHAT: Executed before or after an action result executes<br />DEFAULT: No default resultfilters<br />WH...
AuthorizationFilter<br />ImplementIAuthorizationFilter<br />OnAuthorization<br />
Filters<br />ImplementIActionFilter + IResultFilter<br />MakenewActionFilterAttribute<br />OnActionExecuting<br />OnAction...
Extensibilitypointsyoumightwanttouse<br />
RouteConstraint<br />
RouteConstraint<br />WHAT: Validates route parameters<br />DEFAULT: No default implementation<br />WHY: Addyourownwhenneed...
RouteConstraint<br />Implement<br />IRouteConstraint.Match<br />Inspectrequest and returntrue or false<br />Demo<br />
RouteHandler<br />
RouteHandler<br />WHAT: Defineshow the requestmustbehandled<br />DEFAULT: Routes the requesttoMvcHandler<br />WHY: Changei...
RouteHandler<br />Implement<br />IRouteHandler.GetHttpHandler<br />Demo<br />
ActionMethodSelectorAttribute<br />
ActionMethodSelectorAttribute<br />WHAT: Helps the action invoker decide which action to invoke when the name is not uniqu...
ActionMethodSelectorAttribute<br />Create newAttibuteinheritingfrom<br />ActionMethodSelectorAttribute<br />Implement:<br ...
ActionResult<br />
ActionResult<br />WHAT: Sends the output to the user<br />DEFAULT: ViewResult, RedirectResult, FileResult, JsonResult, etc...
ActionResult<br />Create a new resultinheritingfrom:<br />ActionResult<br />Implement:<br />ExecuteResult<br />Optionally ...
ModelBinder<br />
ModelBinder<br />WHAT: Populates the action method parameters from the request<br />DEFAULT: Bindsrequest’s valuesbased on...
ModelBinder<br />ImplementIModelBinder<br />BindModel<br />InheritfromDefaultModelBinder<br />BindProperty<br />OnModelUpd...
Veryunlikelytowriteyourown<br />
ControllerFactory<br />
Controller Factory<br />WHAT: Responsible for creating Controllers<br />DEFAULT: Create aninstance of the Controller via R...
Controller Factory<br />ImplementIControllerFactory<br />CreateController<br />ReleaseController<br />OverrideDefaultContr...
ActionInvoker<br />
ActionInvoker<br />WHAT: Invokes an action, based only on its name<br />DEFAULT: Call the actionmethod, via reflection, ba...
ActionInvoker<br />Implement IActionInvoker<br />InvokeAction<br />OverrideControllerActionInvoker<br />InvokeActionMethod...
ViewEngine<br />
ViewEngine<br />WHAT: The service that transforms in HTML the data for the user<br />DEFAULT: WebFormviewengine<br />WHY: ...
ViewEngine<br />IViewEngine:<br />UsuallyoverrideVirtualPathProviderViewEngine<br />CreateView<br />IView:<br />Render<br ...
ModelValidatorProvider<br />
ModelValidatorProvider<br />WHAT: Retrieves the validation rules<br />DEFAULT: Validationrules are definedwithDataAnnotati...
ModelValidatorProvider<br />OverridefromModelValidatorProvider<br />ImplementGetValidators<br />
ModelMetadataProvider<br />
ModelMetadataProvider<br />WHAT: Retrieves the metadataneededfor the templatedhelpers<br />DEFAULT: Metadata are definedwi...
ModelMetadataProvider<br />InheritfromModelMetadataProvider<br />Implement<br />GetMetadataForType<br />GetMetadataForProp...
Contacts – Simone Chiaretta<br />MSN: simone_ch@hotmail.com<br />Blog:<br />English: http://codeclimber.net.nz/<br />Itali...
Rating<br />If you liked this talk, please consider rating it:<br />http://speakerrate.com/talks/3669-asp-net-mvc-extensib...
Upcoming SlideShare
Loading in...5
×

ASP.NET MVC Extensibility

4,757

Published on

One of the best features of ASP.NET MVC is that it's totally extensible: if you don't like the way the framework works or if you have scenarios not covered by it, you can change the behaviors by extending the defaults or by writing your owns.
In this presentation I'll go through all the main extensibility points and explain how to leverage the main ones

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,757
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
132
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Explainhow route selectionworks
  • Ifyou are dealingwith CLR types,gowithoverridingDefaultControllerFactory: youdon’t havetoresolve the controllertypebased on thename and youonlyneedto create theinstance of the controller givenitstype. Alldiscovery and filteringisdoneby the defaultimplementationIfyouwanttochange the namingconventions, or are notdealingwith CLR typesyouhaveto go the hard route and implementIController
  • Transcript of "ASP.NET MVC Extensibility"

    1. 1. Extending ASP.NET MVC<br />Part 1<br />Simone ChiarettaArchitect, Council of the EU<br />http://codeclimber.net.nz<br />Twitter: @simonech<br />June 23rd, 2010<br />
    2. 2. Who the hell am I?<br />Simone Chiaretta<br />Microsoft MVP ASP.NET<br />ASP Insider<br />Blogger – http://codeclimber.net.nz<br />ItalianALT.NET UG Founder<br />OpenSource developer<br />Climber<br />All Around Nice Guy<br />Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"<br />
    3. 3. Agenda<br />The default ASP.NET MVC Pipeline<br />Extensibilitypoints’ list<br />The Mostimportantextensibilitypoints<br />
    4. 4. The DefaultASP.NET MVC Pipeline<br />
    5. 5. The default ASP.NET MVC Pipeline<br />4<br />
    6. 6. The default components<br />URL Routing:<br />Parses the URL, andinstantiate the MvcHandler<br />Controller Factory<br />Takes URL parameters, create controller via reflectionbased on Controllername<br />Action Invoker<br />Invokes the actionbased on thename, with the filtersbefore and after<br />ViewEngine<br />WebFormsviewengine<br />Template<br />Renders a TextBoxalmostforeverything<br />HtmlHelper<br />Has a bunch of standardmethods<br />
    7. 7. ASP.NET MVC IS extensible<br />Ifyou don’t likethem<br />Ifyouneedsomething else<br />Changethem<br />
    8. 8. ASP.NET MVC IS extensible<br />Almosteveryaspectof the framework can beextented/replaced<br />
    9. 9. Extensibilitypoints’ list<br />
    10. 10. Routingextensibility<br />RouteConstraint<br />Validatesrouteparameterswith code<br />RouteHandler<br />Defineshow the requestmustbehandled<br />
    11. 11. Controller Extensibility<br />ControllerFactory<br />ResponsibleforcreatingControllers<br />ActionInvoker<br />Invokesanaction, basedonly on itsname<br />ActionMethodSelectorAttribute<br />Helps the actioninvoker decide whichactiontoinvokewhen the nameisnotunique<br />Controller<br />The base classforevery controller<br />ActionResult<br />Decideshowtosend the output to the user<br />
    12. 12. ActionFilterExtensibility<br />AuthorizationFilter<br />Makessure the currentrequestisallowed<br />ActionFilters<br />Executedbefore or after the actionexecutes<br />ResultFilters<br />Executedbefore or afteranactionresultexecutes<br />
    13. 13. Binding/ValidationExtensibility<br />ModelBinder<br />Populates the actionmethodparametersfrom the request<br />ModelValidator Provider<br />Retrieves the validationrules<br />Server-sideValidationRules<br />The actual server-side validationrule<br />
    14. 14. ViewExtensibility<br />ViewEngine<br />The service thattransforms in HTML the data for the user<br />HtmlHelpers<br />Utility functionsthathideaway the generation of some HTML markup or JavaScript code<br />Client-sideValidationRules<br />Client-sidevalidationrules<br />ModelMetadata Provider<br />Retrieves the metadataneededfor the templatedhelpers<br />Custom Templates<br />Renders the html toedit/display specifictypes<br />
    15. 15. The Mostimportantextensibilitypoints<br />
    16. 16. The MostimportantExtensibilitpoints<br />The onesyouwilldefinitelyuse<br />The onesyouhaveto start using<br />The onesyoumight or mightnotneedtouse<br />The onesyoumostlikelywillnotwriteyourself<br />butprobablyusewrittenbyothers<br />
    17. 17. Extensibilitypointsyouwilluse<br />
    18. 18. CustomTemplates<br />
    19. 19. CustomTemplates<br /><ul><li>WHAT: Renders the html toedit/display specifictypes</li></ul>DEFAULT: Everythingis a label or a textbox<br />WHY: Addyourowntocustomizespecificdata-types<br />
    20. 20. CustomTemplates<br />AddPartialViews in:<br />/Views/Shared/DisplayTemplates<br />/Views/Shared/EditorTemplates<br />Demo<br />
    21. 21. Server-sideValidationRules<br />
    22. 22. Server-sideValidationRules<br />WHAT: Definehow a propertyisvalidated in the server side validation<br />DEFAULT: Required, Length, wrong type<br />WHY: Addyourownrules<br />
    23. 23. Server-sideValidationRules<br />Write a newValidationAttribute<br />Implement the IsValidmethod<br />Applyattributetoyourmodel<br />Demo<br />
    24. 24. Client-sideValidationRules<br />
    25. 25. Client-sideValidationRules<br />WHAT: Definehow a propertyisvalidated in the client-side validation<br />DEFAULT: Client-sidecounterparts of the default server-side validators<br />WHY: Addyourownvalidators<br />
    26. 26. Client-sideValidationRules<br />First make a server-side validator<br />Makevalidationlogic in JavaScript<br />Writeadaptertopushvalidationrulesto client-side<br />Registervalidationfunction via JS<br />Registeradapter in Global.asax<br />Demo<br />
    27. 27. Extensibilitypointsyouhaveto start using<br />
    28. 28. Base Controller<br />
    29. 29. Base Controller<br />WHAT: The base class forevery Controller<br />DEFAULT: Defaultimplementation of helpermethods<br />WHY: Extendifyouwanttoenforceyouownconventions<br />
    30. 30. Base Controller<br />Override Controller<br />YourcontrollersoverridefromBaseControllerinsteadof Controller<br />Demo<br />
    31. 31. HtmlHelpers<br />
    32. 32. HtmlHelpers<br />WHAT: Utility functionsthathideaway the generation of HTML markup or JavaScript code<br />DEFAULT: Html.TextBox, Html.Encode, Html.Partial, …<br />WHY: “Ifthereisanif, writeanHelper”<br />
    33. 33. HtmlHelpers<br />Add new methodsasextensionmethods<br />Demo<br />
    34. 34. Rating<br />If you liked this talk, please consider rating it:<br />http://speakerrate.com/talks/3669-asp-net-mvc-extensibility<br />33<br />Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"<br />
    35. 35. Extending ASP.NET MVC<br />Part 2<br />Simone ChiarettaArchitect, Council of the EU<br />http://codeclimber.net.nz<br />Twitter: @simonech<br />June 23rd, 2010<br />
    36. 36. Filters<br />
    37. 37. AuthorizationFilter<br />WHAT: Makes sure the current request is allowed<br />DEFAULT: Itisbased on the ASP.NETMembership provider<br />WHY: ChangeifyouwantnottouseASP.NET MVC or ifyouwanttoenhance the routedictionary<br />
    38. 38. Action Filters<br />WHAT: Executed before or after the action executes<br />DEFAULT: Output cache<br />WHY: Addyourownbased on yourneeds<br />
    39. 39. ResultFilters<br />WHAT: Executed before or after an action result executes<br />DEFAULT: No default resultfilters<br />WHY: Addyourownbased on yourneeds<br />
    40. 40. AuthorizationFilter<br />ImplementIAuthorizationFilter<br />OnAuthorization<br />
    41. 41. Filters<br />ImplementIActionFilter + IResultFilter<br />MakenewActionFilterAttribute<br />OnActionExecuting<br />OnActionExecuted<br />ImplementIResultFilter<br />OnResultExecuting<br />OnResultExecuted<br />
    42. 42. Extensibilitypointsyoumightwanttouse<br />
    43. 43. RouteConstraint<br />
    44. 44. RouteConstraint<br />WHAT: Validates route parameters<br />DEFAULT: No default implementation<br />WHY: Addyourownwhenneeded<br />
    45. 45. RouteConstraint<br />Implement<br />IRouteConstraint.Match<br />Inspectrequest and returntrue or false<br />Demo<br />
    46. 46. RouteHandler<br />
    47. 47. RouteHandler<br />WHAT: Defineshow the requestmustbehandled<br />DEFAULT: Routes the requesttoMvcHandler<br />WHY: ChangeifyouwantnottouseASP.NET MVC<br />
    48. 48. RouteHandler<br />Implement<br />IRouteHandler.GetHttpHandler<br />Demo<br />
    49. 49. ActionMethodSelectorAttribute<br />
    50. 50. ActionMethodSelectorAttribute<br />WHAT: Helps the action invoker decide which action to invoke when the name is not unique<br />DEFAULT: HttpMethod attributes: decide based on the HttpMethod<br />WHY: Addyourowntosupportdifferentscenarios<br />
    51. 51. ActionMethodSelectorAttribute<br />Create newAttibuteinheritingfrom<br />ActionMethodSelectorAttribute<br />Implement:<br />IsValidForRequest<br />Demo<br />
    52. 52. ActionResult<br />
    53. 53. ActionResult<br />WHAT: Sends the output to the user<br />DEFAULT: ViewResult, RedirectResult, FileResult, JsonResult, etc…<br />WHY: Addyourownifyouneedan output notavailable in the default results<br />
    54. 54. ActionResult<br />Create a new resultinheritingfrom:<br />ActionResult<br />Implement:<br />ExecuteResult<br />Optionally create anhelpermethod in you “base controller”<br />Demo<br />
    55. 55. ModelBinder<br />
    56. 56. ModelBinder<br />WHAT: Populates the action method parameters from the request<br />DEFAULT: Bindsrequest’s valuesbased on names<br />WHY: Extendifyouneedtoaddother way ofbinding<br />
    57. 57. ModelBinder<br />ImplementIModelBinder<br />BindModel<br />InheritfromDefaultModelBinder<br />BindProperty<br />OnModelUpdated<br />OnModelUpdating<br />Register the ModelBinder<br />Demo<br />
    58. 58. Veryunlikelytowriteyourown<br />
    59. 59. ControllerFactory<br />
    60. 60. Controller Factory<br />WHAT: Responsible for creating Controllers<br />DEFAULT: Create aninstance of the Controller via Reflectionbased on itsname<br />WHY: Changeifyouwantto create the controller in otherways or ifyouwanttoadd some funtionalities<br />Most of themainIoC container have a customControllerFactory<br />
    61. 61. Controller Factory<br />ImplementIControllerFactory<br />CreateController<br />ReleaseController<br />OverrideDefaultControllerFactory<br />GetControllerInstance(TypecontrollerType)<br />Demo<br />
    62. 62. ActionInvoker<br />
    63. 63. ActionInvoker<br />WHAT: Invokes an action, based only on its name<br />DEFAULT: Call the actionmethod, via reflection, based on itsname, gatheringfilters via attributes and callingthembefore and after<br />WHY: Changeifyouwanttochange the way methods are called, or wantdifferent way toconfigureFilters<br />
    64. 64. ActionInvoker<br />Implement IActionInvoker<br />InvokeAction<br />OverrideControllerActionInvoker<br />InvokeActionMethodWithFilters<br />InvokeActionResultWithFilters<br />GetFilters<br />…<br />Demo<br />
    65. 65. ViewEngine<br />
    66. 66. ViewEngine<br />WHAT: The service that transforms in HTML the data for the user<br />DEFAULT: WebFormviewengine<br />WHY: Changeifyou don’t like the WebFormapproach<br />
    67. 67. ViewEngine<br />IViewEngine:<br />UsuallyoverrideVirtualPathProviderViewEngine<br />CreateView<br />IView:<br />Render<br />Demo<br />
    68. 68. ModelValidatorProvider<br />
    69. 69. ModelValidatorProvider<br />WHAT: Retrieves the validation rules<br />DEFAULT: Validationrules are definedwithDataAnnotationattributes<br />WHY: Changeifyouwanttospecifyvalidationrules in otherways (XML, Database, etc…)<br />
    70. 70. ModelValidatorProvider<br />OverridefromModelValidatorProvider<br />ImplementGetValidators<br />
    71. 71. ModelMetadataProvider<br />
    72. 72. ModelMetadataProvider<br />WHAT: Retrieves the metadataneededfor the templatedhelpers<br />DEFAULT: Metadata are definedwithDataAnnotationattributes<br />WHY: Changeifyouwanttospecifymetadata in otherways (XML, Database, etc…)<br />
    73. 73. ModelMetadataProvider<br />InheritfromModelMetadataProvider<br />Implement<br />GetMetadataForType<br />GetMetadataForProperty<br />GetMetadataForProperties<br />
    74. 74. Contacts – Simone Chiaretta<br />MSN: simone_ch@hotmail.com<br />Blog:<br />English: http://codeclimber.net.nz/<br />Italian: http://blogs.ugidotnet.org/piyo/<br />Twitter: @simonech<br />73<br />
    75. 75. Rating<br />If you liked this talk, please consider rating it:<br />http://speakerrate.com/talks/3669-asp-net-mvc-extensibility<br />74<br />Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"<br />
    1. A particular slide catching your eye?

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

    ×