Razor Generator

This Project has moved! Find it at https://github.com/RazorGenerator/RazorGenerator

This is a Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution.

Note that this tool currently only supports C#. VB support could probably be done if someone wants to help out with it!

Installation instructions

It’s on the VS extension gallery, so install it from there. It’s called “Razor Generator” (not to be confused with “Razor Single File Generator for MVC”).

Generator Types


Usage in an MVC app

Usage in a View Library

If you need to create a separate library for your precompiled MVC views, the best approach is to actually create an MVC project for that library, instead of a library project. You'll never actually run it as an Mvc app, but the fact that it comes with the right set of config files allows intellisense and other things to work a lot better than in a library project.

You can then add a reference to that 'MVC View project' from your real MVC app.

And note that you need to install the 'RazorGenerator.Mvc' package into the library, not the main MVC app.

See https://github.com/davidebbo/MvcApplicationRazorGeneratorSeparateLibrary for a sample.

Special Razor directives

These directives go at the top of the cshtml file. Note that when you have multiple directives, they need to be all on the same line, e.g.

    @* Generator: Template  GeneratePrettyNames : true *@

Generator type

    @* Generator: MvcHelper *@


See above for list of valid values.

disabling line pragmas

    @* DisableLinePragmas: true *@

Using absolute paths in line pragmas instead of relative

    @* GenerateAbsolutePathLinePragmas: true *@

Overriding the generated namespace

    @* Namespace: SomeNamespace *@

Adding a suffix to the generated class name (can be useful to avoid some conflicts)

    @* ClassSuffix: _ *@

Generate namespaces and class names based on file name instead of the Razor default

    @* GeneratePrettyNames : true *@

Trimming leading underscores in generated class names

    @* TrimLeadingUnderscores : true *@


As an alternative, you can create a file named `razorgenerator.directives` in the Views folder to apply directives globally. e.g. it could contain:

    GeneratePrettyNames: true  GenerateAbsolutePathLinePragmas: true

History

9/2/2014 VSIX 1.6.4

1/9/2014 VSIX 1.6.3

12/02/2013 VSIX 1.6.2

11/22/2013 VSIX 1.6.1

11/6/2013 RazorGenerator.Mvc v2.2.1

10/28/2013 VSIX 1.6

8/28/2013 VSIX 1.5.6

8/22/2013 RazorGenerator.Mvc v2.1.2

6/19/2013 RazorGenerator.Mvc v2.1.1

6/5/2013 Runtime v2.1.0

5/4/2013 Runtime v2.0.1

4/25/2013

10/14/2012

4/20/2012

1/8/2012

1.2 (8/29/2011)

1.1.2 (8/4/2011)

1.1.1 (7/11/2011)

1.1 (7/7/2011)

1.0.1 (6/21/2011)

1.0

Related blog posts: