O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Programming Razor

Book Description

Take Razor for a test drive and discover first hand how this scripting syntax simplifies the way you create dynamic, data-driven websites. With this concise guide, you’ll work with Razor syntax by building example websites with Microsoft WebMatrix and ASP.NET MVC. You'll quickly learn how Razor lets you combine code and content in a fluid and expressive manner on Windows-based servers.

Programming Razor also explores components of the Razor API, and shows you how Razor templates are turned into rendered HTML. By the end of this book, you'll be able to create Razor-based websites with custom extensions that meet the specific needs of your projects.

  • Use Razor with Microsoft WebMatrix to build a working blog, complete with data
  • Organize and manage your web application, using Razor features for layouts, sections, and partial views
  • Create clean and effective ASP.NET MVC views with the Razor View Engine
  • Learn how the Razor API interprets Razor templates and turns them into executable .NET code
  • Implement advanced techniques for exposing reusable code and sharing ASP.NET MVC views across projects

Table of Contents

  1. Dedication
  2. A Note Regarding Supplemental Files
  3. Preface
    1. RazorPad: A Lightweight Razor Editor
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Safari® Books Online
    5. How to Contact Us
  4. 1. Introduction
    1. A Brief History of Microsoft’s Web Development Platforms
      1. Active Server Pages (ASP)
      2. ASP.NET Web Forms
      3. ASP.NET MVC
      4. WebMatrix
    2. Hello, Razor!
    3. Differentiating Code and Markup
      1. Code Nuggets
      2. Code Blocks
      3. How Razor Parses Markup and Code
      4. Disambiguating Code and Markup
        1. Explicit code nuggets
        2. The @: character sequence
        3. The <text> block
    4. Comments
  5. 2. Razor and Microsoft WebMatrix
    1. Introducing ASP.NET Web Pages
    2. Installing WebMatrix
    3. Your First WebMatrix Website
      1. The File List View
      2. Website Administration
      3. Hello World, Razor Style
    4. Data Access with WebMatrix
      1. Creating a Database
      2. Populating the Database with Data
      3. Displaying Data from the Database
        1. Creating the Add Post page
      4. Handling Posted Form Data
      5. Saving Data to the Database
      6. Validating Posted Data
        1. Conversion helpers
        2. Using validation
      7. Creating the Home Page
  6. 3. Organizing Razor Templates
    1. Layouts
      1. Layouts Are Pages, Too!
      2. Sections
      3. IsSectionDefined
      4. Nested Layouts
      5. Nested Layouts and Sections
      6. Redefining Sections
      7. The Layout Rendering Life Cycle
      8. Nested Layouts
    2. Partial Views
      1. Creating Partial Views
        1. Accessing parameter values
      2. Reusing Partial Views
    3. Razor Helpers
    4. Razor Helper Packages
    5. Razor Helpers versus Partial Views
      1. Razor Helpers
      2. Partial Views
    6. Executing Common Code
      1. Executing Code the First Time a Page Executes
      2. Executing Code Every Time a Page Executes
      3. Wrapping Views with _PageStart.cshtml Logic
      4. Executing Multiple _PageStart.cshtml Templates
  7. 4. Razor and ASP.NET MVC
    1. Installing ASP.NET MVC
      1. The Model-View-Controller Architecture
    2. ASP.NET MVC View Engines
      1. The Razor View Engine
        1. Differentiating Razor syntax and API implementations
    3. Implementing a Blog Site Using ASP.NET MVC
      1. The Model
      2. The Controller
        1. Data access with Entity Framework code first
      3. Convention versus Configuration
    4. Authoring ASP.NET MVC Views with the Razor Syntax
      1. Adding Razor Views to an ASP.NET MVC Application
      2. Writing ASP.NET MVC Razor View Markup
      3. Strongly-Typed Views
    5. Changing the Base Class
      1. Applying Custom Base Classes to Multiple Views
    6. Layouts and Content Pages
    7. Razor View File Locations
      1. Controller Views
      2. Locating Razor Views
      3. Shared Views
      4. Views in ASP.NET MVC Areas
    8. Html and Url Helper Classes
    9. ASP.NET MVC’s Razor View Page Rendering Life Cycle
      1. Precompiling Razor Views
        1. Precompiling Razor Views in an ASP.NET MVC application
        2. Compiling views from MSBuild
  8. 5. The Razor API
    1. Razor Templates: From Markup to .NET Code
      1. 1. Parse the Razor Template
      2. 2. Generate .NET Code
      3. 3. Compile Generated Code into an Executable Class
      4. 4. Instantiate and Execute the Generated Class
    2. Meet the Players
      1. Configuring the Razor Template Engine
      2. Creating a RazorEngineHost
      3. Creating the RazorTemplateEngine
      4. Compiling Razor Templates
      5. Executing a Razor Template
    3. Advanced Templating Logic
  9. 6. Advanced Techniques
    1. Inline Templates and Templated Delegates
    2. Sharing Views Between Projects
      1. The Razor Single File Generator
        1. Installing the Razor Single File Generator
      2. Creating Reusable ASP.NET MVC Views
        1. Including precompiled views in an ASP.NET MVC web application
      3. Creating Reusable ASP.NET MVC Helpers
      4. Razor Single File Generator Generator Implementations
    3. Unit Testing Razor Views
    4. Applying Razor to Text-Based Scenarios
  10. 7. Conclusion
  11. About the Author
  12. Copyright