Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sitecore MVC Advanced

3,240 views

Published on

This is my presentation for the Sitecore User Group Conference 2015 about Sitecore MVC Advanced.

A video recording is available on YouTube: https://youtu.be/k3Gd1MW1WXI

The source code is available at GitHub: https://github.com/unic/SUGCON2015

Published in: Technology

Sitecore MVC Advanced

  1. 1. Sitecore MVC Advanced Kevin Brechbühl Unic AG @aquasonic Sitecore User Group Conference 2015 1
  2. 2. About • Senior Sitecore developer & architect @unic • Sitecore Technology MVP 2015 • Co-founder of @sugch Sitecore User Group Conference 2015 2
  3. 3. Agenda • ASP.net MVC vs. Sitecore MVC • Multiple Forms • MVC Forms Localization • MVC Areas • There is always more... Sitecore User Group Conference 2015 3
  4. 4. ASP.net MVC vs. Sitecore MVC Sitecore User Group Conference 2015 4
  5. 5. Traditional ASP.net MVC Sitecore User Group Conference 2015 5
  6. 6. Traditional ASP.net MVC Sitecore User Group Conference 2015 6
  7. 7. Sitecore MVC Sitecore User Group Conference 2015 7
  8. 8. Sitecore MVC Sitecore User Group Conference 2015 8
  9. 9. Sitecore MVC Sitecore User Group Conference 2015 9
  10. 10. Sitecore MVC Sitecore User Group Conference 2015 10
  11. 11. Sitecore MVC Sitecore User Group Conference 2015 11
  12. 12. ASP.net MVC • Request correspond to controller action • One view with several partial views • 1 controller & 1 action Sitecore MVC • Request correspond to Sitecore item • Views defined in presentation details • n controllers & n actions Sitecore User Group Conference 2015 12 ASP.net MVC vs. Sitecore MVC
  13. 13. Resources • Comparison from John West http://bit.ly/SitecoreMvcComparison • ASP.net MVC Lifecycle http://bit.ly/MvcLifecycle • Sitecore MVC Pipelines http://bit.ly/SitecoreMvcPipelines Sitecore User Group Conference 2015 13
  14. 14. Multiple Forms Sitecore User Group Conference 2015 14
  15. 15. ASP.net MVC form posts • Each form has 2 actions • Handling GET requests • Handling POST requests • Action has ActionMethodSelector • [HttpGet] or empty • [HttpPost] Sitecore User Group Conference 2015 15
  16. 16. ASP.net MVC form posts Sitecore User Group Conference 2015 16 { CODE }
  17. 17. Issues with Sitecore? • Multiple controllers & multiple actions • Post to same page / url • Each [HttpPost] action is called • Each form is validated • AntiForgeryToken does not work properly Sitecore User Group Conference 2015 17
  18. 18. Solutions • Post via Ajax • Sitecore MVC Form Handler • Custom ActionMethodSelector • As close to ASP.net MVC as possible • None of the described issues Sitecore User Group Conference 2015 18
  19. 19. Custom ActionMethodSelector • Adding a hidden field to the form • Unique Id of the rendering • Post to same page / url • Adding the ActionMethodSelector to the action • Verify the posted rendering id Sitecore User Group Conference 2015 19
  20. 20. Custom ActionMethodSelector Sitecore User Group Conference 2015 20 { CODE }
  21. 21. Resources • Mine: http://bit.ly/MultipleForms1 • Renoud van Dalen: http://bit.ly/MultipleForms2 Sitecore User Group Conference 2015 21
  22. 22. MVC Forms localization Sitecore User Group Conference 2015 22
  23. 23. MVC Forms localization • Ways to translate an ASP.net MVC form • Use .NET language pack • Add static textes • Use .NET resource files • Localization within Sitecore • Sitecore dictionaries Sitecore User Group Conference 2015 23
  24. 24. MVC Forms localization Sitecore User Group Conference 2015 24 { CODE }
  25. 25. Translate labels • Use custom MetaDataProvider • First create the default metadata for a field • Type, Format etc. • Override the DisplayName Sitecore User Group Conference 2015 25
  26. 26. Translate labels Sitecore User Group Conference 2015 26 { CODE }
  27. 27. Translate labels • Conflicts with WFFM MVC • Override the WFFM provider • Register custom provider after WFFM provider Sitecore User Group Conference 2015 27
  28. 28. Translate validation messages • Custom DataAnnotationsModelValidator • Works as an adapter • Each validator type needs a custom adapter Sitecore User Group Conference 2015 28
  29. 29. Translate validation messages Sitecore User Group Conference 2015 29 { CODE }
  30. 30. Resources • Sitecore Usergroup Switzerland • From @studert and me • http://bit.ly/FormsLocalization Sitecore User Group Conference 2015 30
  31. 31. MVC Areas Sitecore User Group Conference 2015 31
  32. 32. Structure of Areas Sitecore User Group Conference 2015 32 Default
  33. 33. Structure of Areas Sitecore User Group Conference 2015 33 Default Area 1
  34. 34. Structure of Areas Sitecore User Group Conference 2015 34 Default Area 2 Area 1
  35. 35. Areas what? • Split models/views/controllers in different areas • Used with multiple sites/layouts in one Sitecore instance • No support by Sitecore, because • No area token in Sitecore default route • Layout termination over pipelines and presentation details Sitecore User Group Conference 2015 35
  36. 36. Solutions • Resolve in mvc.renderRendering pipeline • Use custom ControllerRunner and Renderer • Resolve by <site> Sitecore User Group Conference 2015 36
  37. 37. Resolve in mvc.renderRendering pipeline • Set RouteData.DataTokens["area"] • Controller is then resolved in area • Resolve the area in a custom pipeline • New fields on folders / renderings • Resource • BrainJocks SCORE: http://bit.ly/MvcAreas1 Sitecore User Group Conference 2015 37
  38. 38. Resolve in mvc.renderRendering Sitecore User Group Conference 2015 38 { CODE }
  39. 39. Use custom ControllerRunner • Custom ControllerRunner in mvc.getRenderer pipeline • Also set RouteData-token • Resolve area by new «Area Controller Rendering» • With new AreaControllerRenderer • Resource • Christian van de Luitgaarden: http://bit.ly/MvcAreas2 Sitecore User Group Conference 2015 39
  40. 40. Use custom ControllerRunner Sitecore User Group Conference 2015 40 { CODE }
  41. 41. Resolve by <site> • Adding new route per <site> • Configure with new parameter • Before the Sitecore default route • Add RouteData-token and constraint to each route • Resource • Chris van de Steeg: http://bit.ly/MvcAreas3 Sitecore User Group Conference 2015 41
  42. 42. Resolve by <site> Sitecore User Group Conference 2015 42 { CODE }
  43. 43. But, wait... Sitecore User Group Conference 2015 43
  44. 44. Sitecore Area support • Pattern from BrainJocks • Multiple strategies to resolve the area • Extensible by developers Sitecore User Group Conference 2015 44
  45. 45. There is always more... Sitecore User Group Conference 2015 45
  46. 46. Other resources • Sample Items in MVC http://bit.ly/Sample7Mvc & http://bit.ly/Sample8Mvc • Async Sitecore controllers http://bit.ly/AsyncControllers • Using Dependency Injection to determine MVC Models http://bit.ly/ModelsDI • BrainJocks SCORE http://bit.ly/BJScore Sitecore User Group Conference 2015 46
  47. 47. UserVoice • Sitecore needs your feedback • Share and vote your ideas • Not only for MVC ;-) • http://bit.ly/MvcUserVoice Sitecore User Group Conference 2015 47
  48. 48. /me • Twitter: @aquasonic • Private Blog: http://ctor.io • Sitecore Vibes: http://sitecore.unic.com • Sitecore Usergroup Switzerland: http://sugch.github.io Sitecore User Group Conference 2015 48
  49. 49. Thank you Sitecore User Group Conference 2015 49
  50. 50. Thank you Sitecore User Group Conference 2015 50

×