+ All Categories
Home > Technology > Sitecore MVC Advanced

Sitecore MVC Advanced

Date post: 28-Jul-2015
Category:
Upload: kevin-brechbuehl
View: 550 times
Download: 6 times
Share this document with a friend
Popular Tags:
50
Sitecore MVC Advanced Kevin Brechbühl Unic AG @aquasonic Sitecore User Group Conference 2015 1
Transcript
Page 1: Sitecore MVC Advanced

Sitecore MVC Advanced

Kevin Brechbühl

Unic AG

@aquasonic

Sitecore User Group Conference 2015 1

Page 2: Sitecore MVC Advanced

About

• Senior Sitecore developer & architect @unic

• Sitecore Technology MVP 2015

• Co-founder of @sugch

Sitecore User Group Conference 2015 2

Page 3: Sitecore MVC Advanced

Agenda

• ASP.net MVC vs. Sitecore MVC

• Multiple Forms

• MVC Forms Localization

• MVC Areas

• There is always more...

Sitecore User Group Conference 2015 3

Page 4: Sitecore MVC Advanced

ASP.net MVC vs. Sitecore MVC

Sitecore User Group Conference 2015 4

Page 5: Sitecore MVC Advanced

Traditional ASP.net MVC

Sitecore User Group Conference 2015 5

Page 6: Sitecore MVC Advanced

Traditional ASP.net MVC

Sitecore User Group Conference 2015 6

Page 7: Sitecore MVC Advanced

Sitecore MVC

Sitecore User Group Conference 2015 7

Page 8: Sitecore MVC Advanced

Sitecore MVC

Sitecore User Group Conference 2015 8

Page 9: Sitecore MVC Advanced

Sitecore MVC

Sitecore User Group Conference 2015 9

Page 10: Sitecore MVC Advanced

Sitecore MVC

Sitecore User Group Conference 2015 10

Page 11: Sitecore MVC Advanced

Sitecore MVC

Sitecore User Group Conference 2015 11

Page 12: Sitecore MVC Advanced

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

Page 13: Sitecore MVC Advanced

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

Page 14: Sitecore MVC Advanced

Multiple Forms

Sitecore User Group Conference 2015 14

Page 15: Sitecore MVC Advanced

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

Page 16: Sitecore MVC Advanced

ASP.net MVC form posts

Sitecore User Group Conference 2015 16

{ CODE }

Page 17: Sitecore MVC Advanced

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

Page 18: Sitecore MVC Advanced

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

Page 19: Sitecore MVC Advanced

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

Page 20: Sitecore MVC Advanced

Custom ActionMethodSelector

Sitecore User Group Conference 2015 20

{ CODE }

Page 21: Sitecore MVC Advanced

Resources

• Mine: http://bit.ly/MultipleForms1

• Renoud van Dalen: http://bit.ly/MultipleForms2

Sitecore User Group Conference 2015 21

Page 22: Sitecore MVC Advanced

MVC Forms localization

Sitecore User Group Conference 2015 22

Page 23: Sitecore MVC Advanced

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

Page 24: Sitecore MVC Advanced

MVC Forms localization

Sitecore User Group Conference 2015 24

{ CODE }

Page 25: Sitecore MVC Advanced

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

Page 26: Sitecore MVC Advanced

Translate labels

Sitecore User Group Conference 2015 26

{ CODE }

Page 27: Sitecore MVC Advanced

Translate labels

• Conflicts with WFFM MVC

• Override the WFFM provider

• Register custom provider after WFFM provider

Sitecore User Group Conference 2015 27

Page 28: Sitecore MVC Advanced

Translate validation messages

• Custom DataAnnotationsModelValidator

• Works as an adapter

• Each validator type needs a custom adapter

Sitecore User Group Conference 2015 28

Page 29: Sitecore MVC Advanced

Translate validation messages

Sitecore User Group Conference 2015 29

{ CODE }

Page 30: Sitecore MVC Advanced

Resources

• Sitecore Usergroup Switzerland

• From @studert and me

• http://bit.ly/FormsLocalization

Sitecore User Group Conference 2015 30

Page 31: Sitecore MVC Advanced

MVC Areas

Sitecore User Group Conference 2015 31

Page 32: Sitecore MVC Advanced

Structure of Areas

Sitecore User Group Conference 2015 32

Default

Page 33: Sitecore MVC Advanced

Structure of Areas

Sitecore User Group Conference 2015 33

Default

Area 1

Page 34: Sitecore MVC Advanced

Structure of Areas

Sitecore User Group Conference 2015 34

Default

Area 2

Area 1

Page 35: Sitecore MVC Advanced

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

Page 36: Sitecore MVC Advanced

Solutions

• Resolve in mvc.renderRendering pipeline

• Use custom ControllerRunner and Renderer

• Resolve by <site>

Sitecore User Group Conference 2015 36

Page 37: Sitecore MVC Advanced

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

Page 38: Sitecore MVC Advanced

Resolve in mvc.renderRendering

Sitecore User Group Conference 2015 38

{ CODE }

Page 39: Sitecore MVC Advanced

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

Page 40: Sitecore MVC Advanced

Use custom ControllerRunner

Sitecore User Group Conference 2015 40

{ CODE }

Page 41: Sitecore MVC Advanced

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

Page 42: Sitecore MVC Advanced

Resolve by <site>

Sitecore User Group Conference 2015 42

{ CODE }

Page 43: Sitecore MVC Advanced

But, wait...

Sitecore User Group Conference 2015 43

Page 44: Sitecore MVC Advanced

Sitecore Area support

• Pattern from BrainJocks

• Multiple strategies to resolve the area

• Extensible by developers

Sitecore User Group Conference 2015 44

Page 45: Sitecore MVC Advanced

There is always more...

Sitecore User Group Conference 2015 45

Page 46: Sitecore MVC Advanced

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

Page 47: Sitecore MVC Advanced

UserVoice

• Sitecore needs your feedback

• Share and vote your ideas

• Not only for MVC ;-)

• http://bit.ly/MvcUserVoice

Sitecore User Group Conference 2015 47

Page 48: Sitecore MVC Advanced

/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

Page 49: Sitecore MVC Advanced

Thank you

Sitecore User Group Conference 2015 49

Page 50: Sitecore MVC Advanced

Thank you

Sitecore User Group Conference 2015 50


Recommended