Saturday, July 10, 2010 2:16 PM Kazi Manzur Rashid

Use Razor as ASP.NET MVC ViewEngine

Since the release of Razor I could not manage time to give a deep dive into this new framework. Last night, I started to explore, the idea was to create ASP.NET MVC View Engine, yes I know the official version is coming up end of this month, but it will give me enough details of the Razor internals in the mean time. Part of the goal was to port the default ASP.NET MVC application in Razor. And here is the basic version:

_Master.cshtml

<!DOCTYPE html>
<head>
    <title>
        @RenderSection("title")
    </title>
    <link href="@Href("~/Content/Site.css")" rel="stylesheet" type="text/css"/>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>My ASP.NET MVC Razor Application</h1>
            </div>
            <div id="logindisplay">
                @RenderPage("~/Views/Shared/_LogOnUserControl.cshtml")
            </div> 
            <div id="menucontainer">
                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                </ul>
            </div>
        </div>
        <div id="main">
            @RenderBody()
            <div id="footer"></div>
        </div>
    </div>
</body>
</html>

Home/Index.cshtml

@{ LayoutPage = "~/Views/Shared/_Master.cshtml"; }
@section title {
    Home Page
}
<h2>@ViewData["Message"]</h2>
<p>
    To learn more about ASP.NET MVC Razor Integration visit <a href="http://weblogs.asp.net/rashid">http://weblogs.asp.net/rashid</a>.
</p>

Account/Logon.cshtml

@{ LayoutPage = "~/Views/Shared/_Master.cshtml"; }
@section title {
    Log On
}
<h2>Log On</h2>
<p>
    Please enter your username and password. @Html.ActionLink("Register", "Register") if you don't have an account.
</p>
<form method="post">
    @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div class="editor-label">
                @Html.Label("UserName")
            </div>
            <div class="editor-field">
                @Html.TextBox("UserName")
                @Html.ValidationMessage("UserName")
            </div>
            <div class="editor-label">
                @Html.Label("Password")
            </div>
            <div class="editor-field">
                @Html.Password("Password")
                @Html.ValidationMessage("Password")
            </div>
            <div class="editor-label">
                @Html.CheckBox("RememberMe")
                @Html.Label("RememberMe")
            </div>
            <p>
                <input type="submit" value="Log On"/>
            </p>
        </fieldset>
    </div>
</form>

You will find rest of the view markups in the attached zip file at bottom of this post.

There are quite a few features that it currently does not support:

  1. Strongly typed HtmlHelper (Razor WebPage does not have the support of generic model type).
  2. Render Partial is not working, so you have to use the Razor @RenderPage.
  3. None of the Form Extension is working.
  4. Does not support Razor Start and Init page.

The view engine does not have anything special, except a hack, It turns out the WebPageContext which drives the Razor page rendering has all the member as internal. So I have to use a bit of reflection to set those properties. I wish when can we create a framework without using the internal scope.

To run the attached solution, just open it in the VS2010 and press f5, you do not have to run the IISExpress, it will use the VS built-in web server, Currently it uses C#, but if you want to use VB, just register the ViewEngine in the application_start like the following:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterRoutes(RouteTable.Routes);
    ViewEngines.Engines.Add(new RazorViewEngine(("vb")));
}

That's it for today. Enjoy!!!

Download: AspNetMvcRazor.zip

Shout it
Filed under: , , , ,

Comments

# re: Use Razor as ASP.NET MVC ViewEngine

Saturday, July 10, 2010 3:47 AM by MvcPager

Interesting! then will razor view engine be released?

# re: Use Razor as ASP.NET MVC ViewEngine

Monday, July 12, 2010 7:10 AM by Cvecara Beograd

Interesting engine, it will be interesting seeing it in action

# re: Use Razor as ASP.NET MVC ViewEngine

Tuesday, July 13, 2010 2:42 AM by xsharp

Thanks,How to import namespace???

# re: Use Razor as ASP.NET MVC ViewEngine

Wednesday, July 14, 2010 6:09 AM by asp.net ecommerce development

Great post! Thanks for this code file. I would like to implement it.

# re: Use Razor as ASP.NET MVC ViewEngine

Saturday, July 17, 2010 10:57 PM by Jacky

I found an error,When controller name and view name the same.

ViewData will be null.