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
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
ASP.NET MVC Extensibility
1. Extending ASP.NET MVC Part 1 Simone ChiarettaArchitect, Council of the EU http://codeclimber.net.nz Twitter: @simonech June 23rd, 2010
2. Who the hell am I? Simone Chiaretta Microsoft MVP ASP.NET ASP Insider Blogger – http://codeclimber.net.nz ItalianALT.NET UG Founder OpenSource developer Climber All Around Nice Guy Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"
3. Agenda The default ASP.NET MVC Pipeline Extensibilitypoints’ list The Mostimportantextensibilitypoints
6. The default components URL Routing: Parses the URL, andinstantiate the MvcHandler Controller Factory Takes URL parameters, create controller via reflectionbased on Controllername Action Invoker Invokes the actionbased on thename, with the filtersbefore and after ViewEngine WebFormsviewengine Template Renders a TextBoxalmostforeverything HtmlHelper Has a bunch of standardmethods
7. ASP.NET MVC IS extensible Ifyou don’t likethem Ifyouneedsomething else Changethem
8. ASP.NET MVC IS extensible Almosteveryaspectof the framework can beextented/replaced
11. Controller Extensibility ControllerFactory ResponsibleforcreatingControllers ActionInvoker Invokesanaction, basedonly on itsname ActionMethodSelectorAttribute Helps the actioninvoker decide whichactiontoinvokewhen the nameisnotunique Controller The base classforevery controller ActionResult Decideshowtosend the output to the user
13. Binding/ValidationExtensibility ModelBinder Populates the actionmethodparametersfrom the request ModelValidator Provider Retrieves the validationrules Server-sideValidationRules The actual server-side validationrule
14. ViewExtensibility ViewEngine The service thattransforms in HTML the data for the user HtmlHelpers Utility functionsthathideaway the generation of some HTML markup or JavaScript code Client-sideValidationRules Client-sidevalidationrules ModelMetadata Provider Retrieves the metadataneededfor the templatedhelpers Custom Templates Renders the html toedit/display specifictypes
16. The MostimportantExtensibilitpoints The onesyouwilldefinitelyuse The onesyouhaveto start using The onesyoumight or mightnotneedtouse The onesyoumostlikelywillnotwriteyourself butprobablyusewrittenbyothers
25. Client-sideValidationRules WHAT: Definehow a propertyisvalidated in the client-side validation DEFAULT: Client-sidecounterparts of the default server-side validators WHY: Addyourownvalidators
26. Client-sideValidationRules First make a server-side validator Makevalidationlogic in JavaScript Writeadaptertopushvalidationrulesto client-side Registervalidationfunction via JS Registeradapter in Global.asax Demo
29. Base Controller WHAT: The base class forevery Controller DEFAULT: Defaultimplementation of helpermethods WHY: Extendifyouwanttoenforceyouownconventions
30. Base Controller Override Controller YourcontrollersoverridefromBaseControllerinsteadof Controller Demo
34. Rating If you liked this talk, please consider rating it: http://speakerrate.com/talks/3669-asp-net-mvc-extensibility 33 Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"
35. Extending ASP.NET MVC Part 2 Simone ChiarettaArchitect, Council of the EU http://codeclimber.net.nz Twitter: @simonech June 23rd, 2010
37. AuthorizationFilter WHAT: Makes sure the current request is allowed DEFAULT: Itisbased on the ASP.NETMembership provider WHY: ChangeifyouwantnottouseASP.NET MVC or ifyouwanttoenhance the routedictionary
38. Action Filters WHAT: Executed before or after the action executes DEFAULT: Output cache WHY: Addyourownbased on yourneeds
39. ResultFilters WHAT: Executed before or after an action result executes DEFAULT: No default resultfilters WHY: Addyourownbased on yourneeds
50. ActionMethodSelectorAttribute WHAT: Helps the action invoker decide which action to invoke when the name is not unique DEFAULT: HttpMethod attributes: decide based on the HttpMethod WHY: Addyourowntosupportdifferentscenarios
53. ActionResult WHAT: Sends the output to the user DEFAULT: ViewResult, RedirectResult, FileResult, JsonResult, etc… WHY: Addyourownifyouneedan output notavailable in the default results
54. ActionResult Create a new resultinheritingfrom: ActionResult Implement: ExecuteResult Optionally create anhelpermethod in you “base controller” Demo
56. ModelBinder WHAT: Populates the action method parameters from the request DEFAULT: Bindsrequest’s valuesbased on names WHY: Extendifyouneedtoaddother way ofbinding
60. Controller Factory WHAT: Responsible for creating Controllers DEFAULT: Create aninstance of the Controller via Reflectionbased on itsname WHY: Changeifyouwantto create the controller in otherways or ifyouwanttoadd some funtionalities Most of themainIoC container have a customControllerFactory
63. ActionInvoker WHAT: Invokes an action, based only on its name DEFAULT: Call the actionmethod, via reflection, based on itsname, gatheringfilters via attributes and callingthembefore and after WHY: Changeifyouwanttochange the way methods are called, or wantdifferent way toconfigureFilters
66. ViewEngine WHAT: The service that transforms in HTML the data for the user DEFAULT: WebFormviewengine WHY: Changeifyou don’t like the WebFormapproach
72. ModelMetadataProvider WHAT: Retrieves the metadataneededfor the templatedhelpers DEFAULT: Metadata are definedwithDataAnnotationattributes WHY: Changeifyouwanttospecifymetadata in otherways (XML, Database, etc…)
75. Rating If you liked this talk, please consider rating it: http://speakerrate.com/talks/3669-asp-net-mvc-extensibility 74 Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"
Hinweis der Redaktion
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