Skip to content

Removed RazorTemplateEngine API #25215

@NTaylorMullen

Description

@NTaylorMullen

This is the discussion issue for aspnet/Announcements#433

Removed RazorTemplateEngine API

The RazorTemplateEngine API was removed in favor of RazorProjectEngine.

Version introduced

3.0

Old behavior

A template engine could be created and used to parse and generate code for Razor files

New behavior

A RazorProjectEngine can be created and fed the same type of information as a RazorTemplateEngine to parse and generate code for Razor files. It also provides extra levels of configurability.

Reason for change

RazorTemplateEngine was too coupled to our existing implementations and ultimately led to more questions when trying to properly configure a Razor parsing/generation pipeline.

Recommended action

Use RazorProjectEngine instead of RazorTemplateEngine.

Creating & configuring the RazorProjectEngine

var projectEngine = RazorProjectEngine.Create(
    RazorConfiguration.Default, 
    RazorProjectFileSystem.Create(@"C:/Users/nimullen/source/repos/ConsoleApp4/ConsoleApp4"), 
    builder =>
    {
        builder.ConfigureClass((document, classNode) =>
        {
            classNode.ClassName = "MyClassName";

            // Can also configure other aspects of the class here.
        });

        // More configuration can go here
    });

Generating code for a Razor file

var item = projectEngine.FileSystem.GetItem(@"C:\Users\nimullen\source\repos\ConsoleApp4\ConsoleApp4\Example.cshtml", FileKinds.Legacy);
var output = projectEngine.Process(item);

// Things available
var syntaxTree = output.GetSyntaxTree();
var intermediateDocument = output.GetDocumentIntermediateNode();
var csharpDocument = output.GetCSharpDocument();

Category

ASP.NET

Affected APIs

  • RazorTemplateEngine
  • RazorTemplateEngineOptions

Issue metadata

  • Issue type: breaking-change

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions