DotNetCurry Logo

Using the ViewStart Page in ASP.NET MVC 3 Beta

Posted by: Malcolm Sheridan , on 11/13/2010, in Category ASP.NET MVC
Views: 90926
Abstract: The following article demonstrates how to use the ViewStart page in Razor.

Everyone knows Razor is the new view engine created by Microsoft. Razor syntax is more fluent and readable, and in my opinion, easier to maintain. One feature of Razor that isn’t widely publicised is the addition of the ViewStart page. ViewStart is a file that can store common functionality related to all the views. These pages can also be nested, so if you have multiple controllers, each controller’s views can have their own ViewStart page.

Before moving on, you need to download ASP.NET MVC 3 Beta and NuPack. Click here to download and install them using the Microsoft Web Platform Installer.

Open studio 2010 and create a new ASP.NET MVC 3 Web Application (Razor) project. The new MVC 3 Beta dialog can be seen below.

Asp.Net Mvc3 Project

Choose Razor as the view engine and click OK.

Before doing anything, add a new Razor Layout page under the Shared folder. The next step is to add a new Home controller and create a view. The default HTML can be seen below.

@model dynamic
    View.Title = "Index";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

The code block at the top defines the Title, which is a property of ViewModel and ViewData, and it also defines the layout page via the Layout property. Now imagine you have 50 views and they all contain this code block. All you’re doing is duplicating code. Thankfully you can add a page called _ViewStart.cshtml, or _ViewStart.vbhtml for Visual Basic, and add this code block. By doing this you have the code in one place, and all the views will run _ViewStart before executing. Add a new Razor page under the Views folder and call it _ViewStart.cshtml.

Mvc3 Razor Page 

Cut the code block from the Index view and paste it into the _ViewStart page.

    Layout = "~/Views/Shared/_LayoutPage.cshtml";

If you ran the project you’d see the page running fine.

You can also have nested ViewStart pages. For each folder under the Views folder you can add a _ViewStart page. The benefit of this is each controller’s view can store specific data for the views. The code attached to this article contains nested _ViewStart pages.

I have found some limitations and this may change when MVC 3 is released as a full product. Layout pages can contain sections thanks to RenderSection. You can define a section in your layout page like this:

@RenderSection("footer", required: true)

Using the section is as easy as using the @section keyword in your view.

@section footer {
      <b>copyright 2010</b>

One thing I tried was placing the code above into the _ViewStart page, because you may want the section defined for all views. If you add the @section to the _ViewStart page, you’ll get an exception.

    Layout = "~/Views/Shared/_LayoutPage.cshtml";
@section footer {
      <b>copyright 2010</b>

Define Section Error

The name ‘DefineSection’ does not exist in the current context

I must reiterate this functionality might change when the product is released, but I’m only guessing. ViewStart pages are a great way to group common functionality for your views, and hopefully the functionality you can add to them extends further and further.

The entire source code of this article can be downloaded over here

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Balichin on Wednesday, November 17, 2010 11:56 PM
Great article Malcolm
Comment posted by Michael on Thursday, July 14, 2011 1:29 PM
What is the proper way to define a section in ViewStart.cshtml?
Comment posted by Marijn on Friday, July 29, 2011 4:45 AM
Great article, and exactly the problem I'm facing right now.
Is there a solution or work around available?
I want to use this combined with Area specific _ViewStart files.
Comment posted by vvvvvvvvvvvvv on Tuesday, April 14, 2015 11:48 PM