Book Image

Instant Razor View Engine How-to

By : Abhimanyu Kumar Vatsa
Book Image

Instant Razor View Engine How-to

By: Abhimanyu Kumar Vatsa

Overview of this book

Razor View Engine is an advanced view engine from Microsoft. Razor View is one of the most popular view engines which doesn't require you to explicitly close the code block. This book will help you understand and configure RazorView in your system to create dynamic web pages. Instant Razor View Engine How-to will help you to make your application more MVC friendly with clean, lightweight code which is quicker and easier to understand compared to other view engines. Instant Razor View Engine How-to introduces you to methods to make your web application more MVC friendly. The book begins by detailing the anatomy of RazorView Syntax and configuring Razor View as well as creating a web application project which will also help you to select an appropriate programming language. The later section of the book goes on to discuss creating view templates and creating View pages using Razor syntax. By the end of the book, you will also be familiar with Razor directives, helper components, and work with Partial views to reuse razor code throughout the application.
Table of Contents (7 chapters)

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.