Sitecore MVC
A basic guide for using sitecore MVC. difference between Sitecore MVC and Asp.Net MVC, Difference between Sitecore with Webform and MVC. and different rendering methods specific to Sitecore MVC.
2. What we will see today? Areyouexcited?
• What is Sitecore MVC
• How does it differ from regular .NET MVC?
• Using Web forms and MVC within one solution Sitecore
• Should we use Sitecore MVC?
• Installing and Configuring Sitecore MVC
• Renderings
• @Html.Sitecore() Helper and creating custom Sitecore helper
• Using your own model instead of RenderingModel
3. What is Sitecore MVC?
• A View, It is the component that displays the application's user interface (UI) (all of the things
the user can see and respond to on the screen, such as buttons, display boxes, and so forth).
• A Model, which represents the underlying, logical structure of data in a software application
and the high-level class associated with it at data domain. This object model does not contain
any information about the user interface and the implementation.
• A Controller, which represents the underlying, logical structure of data in a software application
and the high-level class associated with it. This object model does not contain any information
about the user interface.
5. Using Web forms and MVC within one
solution Sitecore
• Yes. We can use both the approach, a traditional web form and even a MVC in a single solution
of Sitecore project.
• I know you guys will have question,
How it will differentiate a traditional Web form request and MVC request in Sitecore?
7. Should we use Sitecore MVC?
• Yes
• No
• Both are true answers.
We should ask question instead,
When we should use Sitecore MVC?
8. MVC Web Forms
• Shorter page lifecycle, better performance • Extra built-in security
• Steep learning curve for web forms developers and junior developers • Faster to build, less classes
• Easy to add DI and more flexible unit testing • Easier to understand for less seasoned .NET developers
* Forces a convention and cleaner coding • Larger knowledge base/ documentation and more tutorials available
• RAZOR view engine is very convenient for front-end integration. • More developers out there with experience in coding with web forms & Sitecore
• Will be supported by Sitecore going forward (we have confirmed this from the
very top)
• Will be supported by Sitecore going forward
• Helps pull new talent, newer framework (with Sitecore) • Will not be made redundant by MVC any time soon
• We would advice that you have a fairly senior .NET development team (That we
have, without any doubt).
• More Sitecore partners are familiar with web forms, so more support available if
short-staffed or something goes wrong
• There are some very large Sitecore clients implementing solutions with MVC,
some of which are already live.
• Tried and tested, most existing Sitecore solutions are built with web forms.
• Very stable, MVC has been around with .NET for a long time, there was not a lot
of core code Sitecore needed to change to support MVC
• Very stable
• Good de-coupling from HTML, no need to worry about automatic ID's generated
by web-forms controls, resulting in cleaner HTML output.
• Fastest approach to get a clean application live for a company with predominantly
web forms experience.
10. View Rendering
A view rendering consists of a view that takes a RenderingModel by default. The model is
assembled by the Sitecore MVC pipeline, and you can create your own, custom models. Because
the pipeline expects an Initialize() method on your model, you can either inherit from
RenderingModel or implement IRenderingModel.
To post a view rendering, specify the controller and action that you want to post to on the
component’s definition item.
Advantages
• They are simple to use – especially if you do not use a custom model. Most of the time, you only
want to display some content from the context or data source item without requiring any
business logic – a simple ‘page’ will often consist of a title and some text, which you can do very
easily with Sitecore’s RenderingModel and the @Html.Sitecore() helper. There isn’t much to unit
test here (you can certainly do web tests), and creating a controller action seems extraneous.
11. • When you post a form from a view rendering, it will only post the action you have specified – after which you
can redirect to another page or trigger the Sitecore rendering pipeline. This is not the case with controller
renderings, which make posting forms a bit more difficult.
Disadvantages
• If you need to do any business logic when retrieving your model, you will want to do that in a separate layer.
However, because you do not have access to a controller, the only place you can do this logic is in the
Initialize() method of a custom model – and putting business logic in the model itself is not ideal.
• It’s just not very ASP.NET MVC. Developers are used to having a controller and an action, and view renderings
do not work like that. However, you must keep in mind that Sitecore MVC is an implementation of ASP.NET
MVC that needs to support the same features as its Web Forms counterpart. The modular nature of a Sitecore
page introduces challenges.
12. Controller Rendering
Rather than specifying a view, the component definition item for a controller rendering specifies
a controller name and an action name. When the rendering is added to a placeholder, this
action is executed and the view is returned.
Advantages
• They look ASP.NET MVC. You can use dependency injection and common patterns (like the
repository pattern) as you normally would, and create your own actions.
• Keeps your business logic separate from your model; you can create plain POCO classes that are
hydrated by a business logic layer.
13. Disadvantages
• Although they look ASP.NET MVC, they are still renderings and will not behave exactly as a controller
action might in a standard ASP.NET MVC application. This is particularly evident when you try to have
more than one post on the page, or attempt to RedirectToAction.
• They are executed after the main view has already been rendered, which results in some
unexpected behavior – again, this might cause some strange behavior when you try to post a form,
and with TempData (if you use that).
• In some instances, a controller rendering is a lot of unnecessary work