A WebForm View with a Razor Layout in ASP.NET MVC 3
Introduction:
By default, ASP.NET MVC 3 ships with two built-in view engines: WebFormViewEngine and RazorViewEngine. WebFormViewEngine is for web form view and RazorViewEngine is for razor view. You can use a master page to create a consistent layout for your web form views and a razor layout for your razor views. But sometimes you may need to leverage a razor layout for your web form views. In this article, I will show you a trick that allows you to use a razor layout for your web form views.
Description:
First of all, create an ASP.NET MVC 3 application. Next, add a controller, a web form view and a razor layout. Here is the code for controller,
01 | public class HomeController : Controller |
03 | public ActionResult Index() |
05 | ViewBag.Message = "Welcome to ASP.NET MVC!" ; |
10 | public ActionResult About() |
Here is the web form view,
01 | <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ConnectRazorLayoutWithWebFormView.Master" Inherits="System.Web.Mvc.ViewPage< dynamic >" %> |
03 | < asp:Content ID = "Content1" ContentPlaceHolderID = "TitleContent" runat = "server" > |
04 | A WebForm View with a Razor Layout |
07 | < asp:Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server" > |
09 | < h2 >A WebForm View with a Razor Layout</ h2 > |
and here is the razor layout,
04 | < title >@ViewBag.Title</ title > |
05 | < link href = "@Url.Content(" mce_href = "@Url.Content(" ~/Content/Site.css")" rel = "stylesheet" type = "text/css" /> |
06 | < script src = "@Url.Content(" mce_src = "@Url.Content(" ~/Scripts/jquery-1.5.1.min.js")" type = "text/javascript" ></ script > |
12 | < h1 >My MVC Application</ h1 > |
14 | < div id = "logindisplay" > |
15 | @Html.Partial("_LogOnPartial") |
17 | < div id = "menucontainer" > |
19 | < li >@Html.ActionLink("Home", "Index", "Home")</ li > |
20 | < li >@Html.ActionLink("About", "About", "Home")</ li > |
Note above that the master page of above web form view is ConnectRazorLayoutWithWebFormView.Master. This master page will be used to leverage a razor layout for above web form view. Here is the ConnectRazorLayoutWithWebFormView.Master file,
01 | <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> |
03 | < asp:PlaceHolder runat = "server" Visible = "false" >< asp:ContentPlaceHolder ID = "TitleContent" runat = "server" /></ asp:PlaceHolder > |
04 | < asp:PlaceHolder runat = "server" Visible = "false" >< asp:ContentPlaceHolder ID = "MainContent" runat = "server" /></ asp:PlaceHolder > |
06 | var buildTitle = new StringBuilder(); |
07 | var buildMain = new StringBuilder(); |
08 | TitleContent.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(buildTitle))); |
09 | ViewBag.Title = buildTitle.ToString().Trim(); |
10 | MainContent.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(buildMain))); |
11 | ViewBag.MainContent = buildMain.ToString().Trim(); |
13 | <%= Html.Partial("ConnectRazorLayoutWithWebFormView", viewData: ViewData)%> |
The ConnectRazorLayoutWithWebFormView master file contains two ContentPlaceHolder controls and each ContentPlaceHolder control is inside a PlaceHolder control with Visible="false". Setting the Visible property of PlaceHolder control to false means that the control does not render any HTML. Then, inside server tags, I am explicitly rendering Title and Main ContentPlaceHolder controls, and saving them in ViewBag.Title and ViewBag.MainContent. This master file also include a partial view(ConnectRazorLayoutWithWebFormView.cshtml), which will connect the above razor layout with the above web form view. Here is the ConnectRazorLayoutWithWebFormView.cshtml file contents,
2 | Layout = "~/Views/Shared/_Layout.cshtml"; |
5 | @Html.Raw(ViewBag.MainContent) |
This view is simply setting the Layout property, which will connect the above razor layout(_Layout.cshtml) with the above web form view(Index.aspx) and rendering ViewBag.MainContent.
Summary:
In ASP.NET MVC 3, you can use master page to create a layout for web form views and a razor layout for razor views. But sometimes you may need to use a razor layout for your web form views. In this article, I showed you how you can connect a razor layout with a web form view. Hopefully you will enjoy this article too. I am attaching a sample application.