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”)

Last edited Jun 23, 2011 at 7:40 AM by davidebbo, version 2

Comments

MikhailGirshberg Mar 31, 2016 at 9:07 AM 
Fixed the problem with building. After system(!) reboot I was able to add references to namespaces manually with exception for "System.Web.Security" and 'System.Web.UI'. Are they needed in this example?

MikhailGirshberg Mar 28, 2016 at 5:30 PM 
Given example (taken literally) does not work for me.
'Razor Generator' produces a file with references to unresolved namespaces like 'System.Web.Helpers'.
Is 'creating a new Class Library project in Visual Studio' enough or some environment prerequisites supposed?

blaiseliu Jun 10, 2014 at 3:33 PM 
Yes, please update the first screen shot. It is too confusing.

eccdan May 22, 2014 at 2:43 PM 
Should a mention about the Nuget Package (RazorGenerator.Templating) be added somewhere in this guide? The only mention of it I could find is in this 3 years old thread: http://razorgenerator.codeplex.com/discussions/271171

gregoryagu Mar 10, 2014 at 3:23 PM 
Note that the RazorGenerator is a Visual Studio Extension, NOT the NuGet package. NuGet has a package with the exact same name, which can be confusing. So if you are not getting the compiled file, try installing the Extension as outlined above.

steveculshaw Oct 1, 2013 at 11:56 AM 
Seems like the screen shot of the NuGet response is out-of-date ... should it now be the RazorGenerator.MVC?

jwize Aug 13, 2011 at 10:56 PM 
Is there any way to use your own base template using this project?