The latest stable release of the Razor Generator is available on the VS Extension Gallery.  Once you’ve installed the extension, restart VS. VsixGallery

 

The Single File Generator is available as a Custom Tool in Visual Studio that compiles a cshtml files (we don’t support vbhtml as yet) to a class file. The tool supports multiple generators or hosts that tweaks the generated type for specific purposes. We’ll start with a simple host – MvcHelper – that creates a static type with MVC-specific imports.

 

1) Start by creating a new Class Library  project in Visual Studio and add a Razor file with the extension .cshtml.

@* Generator : MvcHelper *@
 
@helper WriteSpan(this HtmlHelper helper, string text) {
    <span>@text</span>
}

Note the Razor comment in the first line of the file. This tells the generator what host to use.

 

2) Under the properties of the file, modify the Build Action to None and set the Custom tool to RazorGenerator.

Properties

 

You’ll notice that a nested file is added to your project.

Nesting

 

3) The nested file will be compiled with your project and can be referenced as a regular helper. e.g. Html.WriteSpan(“Hello world”)