Sitecore is making a move: the shift from Sitecore's web forms to Sitecore MVC has been a gradual one, but it's ramping up quickly. Stay in the know with this primer on what it is and why it's important
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Sitecore MVC: What it is and why it's important
1. What is Sitecore MVC?
Out on the razor’s edge
Prepared by: Jason St-Cyr and Nick Allen
2. What is Sitecore MVC?
• MVC as you may know it
• Sitecore MVC
• Why bother moving to Sitecore MVC?
Resources
3.
4. MVC as you may know it
• ASP.NET MVC
- Model, View, Controller pattern
- Interactions are routed through controllers, no post-backs
- Views mapped to REST-friendly URLs
- Light-weight, highly testable framework
- No View State or server-based forms
- Razor as the view engine
- Convention over configuration
Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 4
5. MVC as you know it: Route registration
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
6. MVC as you know it: Controllers
public class CustomerController : Controller
{
//
// POST: /Customer/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
7. Sitecore MVC
• Sitecore MVC != ASP.NET MVC
- Can use MVC routing, but defaults to using Sitecore as routing engine
- Sitecore constructs page based on placeholder settings and
presentation details, just like with Web Forms
- Individual components are defined as Controller Renderings or View
Renderings
- Instead of ‘controller-per-page’, Sitecore uses ‘controller-per-rendering’
- Sitecore will run in “MVC mode” if:
a) An MVC route is matched
b) Item layout is an MVC view
Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 7
8. Sitecore MVC: Controller vs. View Renderings
• Controller Rendering
• Executes Controller you
build
• Can specify relative or fully
qualified class for
controller
• Controller logic builds
model and determines
views
• Controller is similar to
code-behind of an ASCX
• View Rendering
• Sitecore becomes the
Controller
• Directly binds to an
associated .cshtml file
• Requires a model item,
which binds directly to
rendering model class
• Useful for ‘presentation-only’
renderings (e.g. layout
renderings)
Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 8
9. Why bother?
• Sitecore is going MVC
- Sitecore 8: all UIs in SPEAK
• Developers want to work with MVC solutions
- Separation of concerns (presentation vs code-behind)
- Makes unit testing easier (if you do it right)
- Re-use logic and models more easily between views
- More control over rendered HTML (no view state garbage)
• Clients are beginning to ask for MVC solutions
- Doing both > Only web forms
• Web Forms will go away (many years from now, but still…)
Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 9
11. Sitecore Community Docs
• http://sitecore-community.
github.io/docs/documentation/Sitecore%20MV
C/index.html
• Contains links to online content submitted by the Sitecore
community.
• Key links:
- Anything by Martina Welander
- Creating a Visual Studio Project for Sitecore MVC
- Sample Sitecore MVC project (used in Youtube tutorial videos)
Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 11
12. MSDN and others
• ASP.NET MVC Overview:
- http://msdn.microsoft.com/en-us/
library/dd381412(v=vs.100).aspx
• Getting Started with ASP.NET MVC 5
- http://www.asp.net/mvc/overview/getting-started/
introduction/getting-started
• PluralSight ASP.NET MVC 5 Fundamentals
- http://www.pluralsight.com/courses/aspdotnet-mvc5-
fundamentals
13. Jason St-Cyr
Solution Architect and Sitecore MVP, nonlinear digital
Lessons Learned
Background in .NET software development and Application Lifecycle
Management
Contact me: jst-cyr@nonlinear.ca
Around the web:
Technical Blog:
http://theagilecoder.wordpress.com
LinkedIn Profile:
http://www.linkedin.com/pub/jason-st-cyr/26/a73/645
Nonlinear Thinking:
http://www.nonlinearcreations.com/Digital/how-we-think
Twitter: @AgileStCyr
14. Nick Allen
Solution Architect and Sitecore MVP, nonlinear digital
Lessons Learned
Background in .NET software development
Contact me: nallen@nonlinear.ca
Around the web:
Technical Blog:
http://sitecorecreative.wordpress.com
LinkedIn Profile:
https://www.linkedin.com/in/nickallen80
Nonlinear Thinking:
http://www.nonlinearcreations.com/Digital/how-we-think
Twitter: @sitecoretweet