Reader small image

You're reading from  Instant Razor View Engine How-to

Product typeBook
Published inMay 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781849696302
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Abhimanyu Kumar Vatsa
Abhimanyu Kumar Vatsa
author image
Abhimanyu Kumar Vatsa

Abhimanyu Kumar Vatsa works at Coxtan College, located in Bokaro Steel City, India as a lecturer focused on web technologies. He is a Microsoft MVP in ASP.NET/IIS. He loves to blog and started blogging in June 2009. He holds a bachelor's degree in Computer Science and Applications, a master's degree in Information Technology, and a few application-level diplomas. He moved to Bokaro Steel City from a remote village to pursue higher education in March 2003, and that was when he saw a computer with Windows 95 OS on Pentium 1 for the first time, and since then, he has never looked back.
Read more about Abhimanyu Kumar Vatsa

Right arrow

Introduction


Let's quickly start by looking at what MVC architecture is and how it is different from Web Forms.

The Model-View-Controller (MVC) pattern is an architectural design principle that separates the components of a web application. This separation gives more control over the individual parts of the application, which lets us develop, modify, and test them easier. MVC is part of the ASP.NET framework. Developing an MVC application is an alternative to developing ASP.NET Web Forms pages; it does not replace the Web Forms model.

ASP.NET MVC comes with two view engines by default; ASPX (also known as Web Forms view engine) and Razor. The view engine is responsible for creating HTML from our views. Views are usually a mixture of HTML and a programming language. The Razor view engine was introduced as part of MVC 3 and the Microsoft WebMatrix toolset.

Razor is not a new language; it is the response to one of the most requested suggestions received by the MVC team to provide a clean, lightweight, quicker, and friendlier view engine that minimizes the number of characters and keystrokes required in a file and enables a fast and fluid coding workflow.

If you select Visual Basic as your programming language, when you add a new view in your project, you will be offered to select either ASPX (VB) or Razor (VBHTML) view engines, as shown in the following screenshot:

If you select Visual C# as your programming language, when you add a new view in your project, you will be offered to select either ASPX(C#) or Razor (CSHTML) view engines, as shown in the following screenshot:

The ASPX view engine can have either a .aspx or .ascx (user control) file extension, and the Razor view engine can have either a .cshtml (for C#) or .vbhtml (for VB) file extension; this depends on what programming language you have selected for your project. One of the things that you will love is the choice and flexibility for view engine selection while adding the view. You can use different view engines in the same project/application; it is all up to you.

Other popular view engines used today are Spark and NHaml (pronounced "enamel"); these are pluggable view engines in MVC.

Why Razor?

In short, using Razor is rather wrapping your code elements in view with <% %> tags, for which you can simply use an @ symbol. Actually, the Razor view engine has syntactic awareness built into it, thus there is no need to close your tags. If you want to declare multiple lines of code you can wrap your code in the following symbols @{code goes here}. This will go a long way to reduce the tag soup that you get in your views.

Versions

When the Razor view engine was introduced with MVC 3 in January 2011, it was Version 1. The latest version of Razor is v2, which was released with ASP.NET MVC 4. Razor v2 includes a bunch of new features that we will cover throughout this book.

Prerequisites

The minimum requirement to program with Razor syntax is Visual Studio 2010 or Visual Web Developer 2010. These versions will offer you the promised IntelliSense support. However, I will recommend you to download the latest version available. As of now, the latest version is Visual Studio 2012 or Visual Web Developer 2012 available from the Microsoft website. So, whatever Visual Studio version you use, you need to have MVC 3 or MVC 4 project templates.

Previous PageNext Page
You have been reading a chapter from
Instant Razor View Engine How-to
Published in: May 2013Publisher: PacktISBN-13: 9781849696302
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Abhimanyu Kumar Vatsa

Abhimanyu Kumar Vatsa works at Coxtan College, located in Bokaro Steel City, India as a lecturer focused on web technologies. He is a Microsoft MVP in ASP.NET/IIS. He loves to blog and started blogging in June 2009. He holds a bachelor's degree in Computer Science and Applications, a master's degree in Information Technology, and a few application-level diplomas. He moved to Bokaro Steel City from a remote village to pursue higher education in March 2003, and that was when he saw a computer with Windows 95 OS on Pentium 1 for the first time, and since then, he has never looked back.
Read more about Abhimanyu Kumar Vatsa