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

Publisher resources

View/Submit Errata

Table of contents

  1. Programming Razor
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. 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
    3. 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
    4. 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
    5. 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
    6. 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
    7. 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
    8. 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
    9. 7. Conclusion
    10. About the Author
    11. SPECIAL OFFER: Upgrade this ebook with O’Reilly

Product information

  • Title: Programming Razor
  • Author(s): Jess Chadwick
  • Release date: September 2011
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449317164