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

This book will begin with an introduction to the Razor syntax, where/how it fits in to the Web Matrix and ASP.NET MVC platforms, and how to effectively leverage the syntax on those platforms. It will continue to dive deeper, espousing some best practices and useful sample code for the Web Matrix and ASP.NET MVC platforms. Finally, the book will examine the underlying APIs themselves (which are separate from the syntax discussed previously), explain how the Razor API works, and show how to extend the API with custom functionality, even in non-web scenarios.

Table of Contents

  1. Dedication
  2. Special Upgrade Offer
  3. A Note Regarding Supplemental Files
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 7. Conclusion
  12. About the Author
  13. Special Upgrade Offer
  14. Copyright