Meet the Players

Now that you know how a Razor template gets turned into an executable class, it’s time to take a tour of the Razor API classes that make it all possible. The basic list is surprisingly short:

System.Web.Razor.Parser.RazorParser

RazorParser executes the logic discussed in How Razor Parses Markup and Code, mapping a Razor template into an in-memory document that the RazorCodeGenerator can consume. Despite its name, RazorParser does not know anything about HTML or code languages such as C#. Instead, RazorParser relies on other code and markup implementations to perform the actual parsing. For example, the MarkupParser class knows how to interpret HTML markup, and the CSharpCodeParser and VBCodeParser classes know how to parse C# and VB.NET code, respectively. RazorParser merely acts as the coordinator between various markup and code parsing implementations.

System.Web.Razor.Generator.RazorCodeGenerator

The RazorCodeGenerator class contains the logic for generating .NET code from RazorParser’s output. Like RazorParser, the RazorCodeGenerator class itself does not have any knowledge of any specific languages, only general knowledge of how to interpret RazorParser output. Unlike RazorParser, however, RazorCodeGenerator is an abstract base class from which language-specific implementations (such as CSharpRazorCodeGenerator and VBRazorCodeGenerator) derive. Razor API consumers reference the language-specific implementations that derive from RazorCodeGenerator, not RazorCodeGenerator ...

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.