The Razor View Engine
The new view engine for ASP.NET MVC and WebMatrix combines simplicity and functionality to facilitate clean view development. In this article, I’ll dive into the Razor View Engine, the new default view engine for the ASP.NET MVC framework and WebMatrix products. Razor’s main goal is to simplify view development and to improve developer productivity while providing a clean view infrastructure.
The ASP.NET MVC product is under constant change and the introduction of a new view engine is not a surprising announcement given the breadth of knowledge that Microsoft has on its side in regards to MVC. Having acquired Hamilton Verissimo (author of the CastleMonorail) and Louis DeJardin (author of the Spark View Engine), the Razor view engine announcement should not surprise you. The Razor view engine blends the gap between the WebForms view engine and the other popular view engines available today.
This article dives into the new default view engine of ASP.NET MVC, Razor. I will start with basic notation and move into more advanced usages with sections, HTML helpers and the layout files. A basic understanding of ASP.NET MVC is recommended as this is not an introduction to ASP.NET MVC or WebMatrix. All examples (except for one) will be given in the context of C#, however, Visual Basic is supported for developing Razor views.
Yet Another View Engine?
During the short life of ASP.NET MVC you’ve been introduced to many view engines - WebForms (default in version 1 and 2 of ASP.NET MVC), Spark, NHaml, NVelocity and many others. The big question you’re probably asking is - why do I need another view engine? The simple answer is - iteration. The ASP.NET MVC team cranks out releases of their products probably faster than any other department in the Developer Division at Microsoft. One very admirable quality that they possess is their great ability to listen to the community when they speak up. For this release, the ASP.NET MVC team listened to the community when they spoke up about the verbosity of syntax that the WebForms view engine required. Mainly users were tired of typing quite verbose statements to accomplish simple things such as outputting variable to the response. A simplified example of such code is as follows: <%= name %>
The code above outputs the value of the name variable to the response stream. The same code, in Razor is shown below: @name
Razor not only supplies simplicity in view development but it also saves keystrokes and improves readability inside of the HTML. This is because of the lack of brackets in the Razor language as juxtaposed with the WebForms view engine which required angle brackets to write to the response or enter code notation.
Best of all, you’re given options. You’re not required to choose the Razor view engine (however it is the default in ASP.NET MVC 3) as shown in Figure 1.
Figure 1: When adding a new view, you’re given options to what view engine you’d like to use.
By: Donn Felker
Donn Felker is the Principal for Agilevent, a Microsoft Partner he founded in Minneapolis, Minnesota. He is an experienced Software Architect/Developer with over nine years of professional experience in various markets that include: entertainment, health, retail, insurance, financial, and real estate.
He is a Microsoft ASP Insider who currently holds certifications as an MCTS in Web Client Development for the ASP.NET 3.5 Framework, Certified Scrum Master and ITIL Foundation v2. He also holds a bachelors of science in Software Engineering. He writes, presents and consults on various topics ranging from architecture to agile practices to patterns and practices. He is the founder/coordinator of the Twin Cities Give Camp, President of the Twin Cities Developers Guild and the founder of Twin Cities Pragmatic Beer.
You can read Donn’s blog at http://blog.donnfelker.com