Chapter 1. Introduction

HyperText Markup Language (HTML) makes the World Wide Web possible. Every website uses HTML to render content, and much of HTML’s popularity derives from its simplicity: with knowledge of just a few concepts, anyone can publish content to the Web.

HTML may be a way of life for web developers, but when it comes to creating dynamic, data-driven websites, most developers turn to some kind of tool to make the job of generating HTML easier. Razor is one of those tools: a scripting syntax for making templates and web content on Windows-based web servers.

This book is designed to get you acquainted with the Razor syntax and how it fits into the two primary Microsoft development environments: ASP.NET MVC and WebMatrix. The final chapters will dive deeper, cracking open the underlying tooling and API to see what makes this all possible. By the end of this book, not only will you know how to create great Razor-based websites, but you will also be able to add custom extensions and make Razor even better suited to the specific needs of your projects!

A Brief History of Microsoft’s Web Development Platforms

Long ago, Microsoft saw the need for a Windows-based web development platform and worked hard to produce a solution. Over the last two decades, Microsoft has given the development community several web development platforms.

Active Server Pages (ASP)

Microsoft’s first answer to web development was Active Server Pages (ASP), a scripting language in which code and markup are authored together in a single file, with each physical file corresponding to a page on the website. ASP’s server-side scripting approach became widely popular and many websites grew out of it; some continue to serve visitors today! After a while, though, developers wanted more: things like easier code reuse, better separation of concerns, and easier application of object-oriented programming principles. In 2002, Microsoft offered ASP.NET as a solution to these concerns.

ASP.NET Web Forms

Like ASP websites, ASP.NET websites rely on the page-based approach, where each page on the website is represented in the form of a physical file (called a Web Form) and is accessible using that file’s name. Unlike a page using ASP, a Web Forms page provides some separation of code and markup by splitting the web content into two different files: one for the code and one for the markup. ASP.NET and the Web Forms approach served developers’ needs for years and continues to be the web development framework of choice for many .NET developers. Some .NET developers, however, consider the Web Forms approach too much of an abstraction from the underlying HTML, JavaScript, and CSS. Gee, some developers just can’t be pleased! Or can they?

ASP.NET MVC

Microsoft was quick to spot the growing need in the ASP.NET developer community for something different from the page-based Web Forms approach, and it released the first version of ASP.NET MVC in 2008. Representing a total departure from the Web Forms approach, ASP.NET MVC abandons the page-based architecture completely, using a Model-View-Controller (MVC) architecture instead. Though it still leverages much of the previous framework, ASP.NET MVC represents an entirely separate stack. Instead of markup files, views take the responsibility for rendering HTML to the user. ASP.NET MVC leaves it up to application developers to choose the syntax they use to author views. Razor is quickly emerging as the most popular ASP.NET MVC view syntax for reasons that should become quite clear while reading this book!

WebMatrix

Released at the same time as ASP.NET MVC 3 in early 2011, WebMatrix is Microsoft’s simple, straightforward, and free web development environment. Comprising a simple integrated development environment (IDE) and an API (named Web Pages), WebMatrix is a natural fit in the evolution of Microsoft’s web development frameworks. WebMatrix offers a middle ground for those who view ASP as a hindrance to object-oriented development, ASP.NET Web Forms as too much of an abstraction from core HTML/CSS/JavaScript, and ASP.NET MVC as too complex.

At a glance, WebMatrix web pages bear a strong resemblance to ASP web pages, in that they combine business logic and markup in the same file. However, if you dig deeper, you’ll quickly find a very object-oriented foundation lurking underneath. By combining the power of the ASP.NET platform with the simplicity of ASP-like scripting syntax (the Razor syntax), WebMatrix offers a web development environment that is approachable by a very broad range of website developers. WebMatrix is straightforward enough to allow a hobbyist to produce a simple website, yet powerful enough to satisfy the needs of more advanced web applications.

Get Programming Razor now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.