How to Use a Razor Generator to Automate Your Code

Written by

in

Razor Generator is a popular custom tool for Visual Studio and MSBuild that parses Razor markup files (.cshtml) at design or build time instead of waiting until runtime. By turning standard markup into precompiled C# classes, you can automate text generation, build reusable view libraries, eliminate cold-start performance delays, and easily unit-test your layouts.

Using it allows you to easily treat Razor as a high-powered alternative to T4 text templates. 1. Select Your Automation Template Type

The tool converts markup using specific generators depending on your project goal:

Template: Turns Razor into a standalone code-generation or text-generation class. Ideal for automating email generation, creating source code files, or building SQL script builders.

MvcView: Precompiles standard web views so they can be bundled directly into shared .dll class libraries for distribution.

MvcHelper / WebPagesHelper: Generates static classes designed to house reusable, strongly typed UI snippets. 2. Install the Required Tools

To get the automation environment working seamlessly, install both the extension and the build hooks. RazorGenerator – GitHub

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *