ASP NET MVC Razor View Engine

Feeds

RSS Feed

<< June | July | August >>

Tuesday, 6th July 2010

ASP.NET MVC was a massive step forward for Microsoft developers and all those who have taken advantage of the wonderful stateless, separated world of MVC should prepare themselves for the next logical progression, the Razor view engine.

The only bad thing you can say about ASP.NET MVC is that your views can get a bit noisy. Each time you drop some dynamic data into the HTML page, you end up with loud code-blocks that make the page untidy and hard to read. Well, with the introduction of Razor you can clean up your act and make your HTML views the very embodiment of serenity.

Here is an everyday example of how it will improve the readability of your view - the example is taken from an open source application to make it more real-to-life, but don't take any coding style from the example - just check out the difference between the two!

Before Razor...


                <section>
                    <p><%= Model.DescriptionForTaskPerson %> 
			<span><%= taskDetail.Person.FirstName %> <%= taskDetail.Person.LastName %></span></p>
                    <p><%= Model.DescriptionForTaskPriority %> 
			<span><%= taskDetail.Priority.Title %></span></p>
                    <p><%= Model.DescriptionForRelease %> 
			<span><%= taskDetail.Task.Release %></span></p>
                    <p><%= Model.DescriptionForMilestoneDate %> 
			<span><%= taskDetail.Task.MilestoneDate.ToString("dd/MM/yyyy") %></span></p>
                </section>

After Razor...


                <section>
                    <p>@Model.DescriptionForTaskPerson
			<span>@taskDetail.Person.FirstName @taskDetail.Person.LastName</span></p>
                    <p>@Model.DescriptionForTaskPriority
			<span>@taskDetail.Priority.Title</span></p>
                    <p>@Model.DescriptionForRelease
			<span>@taskDetail.Task.Release</span></p>
                    <p>@Model.DescriptionForMilestoneDate
			<span>@taskDetail.Task.MilestoneDate.ToString("dd/MM/yyyy")</span></p>
                </section>

Razor should be available in public beta very soon according to Scott Guthrie, and you can read Scott Guthrie's post about Razor here.

You Are Here: Home » Blog » ASP NET MVC Razor View Engine