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.
Clone or download
svrx and pranavkm Updated tooling dependencies on System.Web.* to latest versions (#148) (
#149)

* Updated tooling dependencies on System.Web.*  to latest versions (#148)

* Removed the requirement for a specific version from the v3 core

* Bumped MsBuild package version to 2.5.0
Latest commit 032180e Jun 8, 2017
Permalink
Failed to load latest commit information.
.nuget Swtich to NuGet v3.3.0 Apr 28, 2016
MsBuildTest Adding test to verify MsBuild package Mar 30, 2015
MvcViewsTests Normalize all the line endings Jan 20, 2016
PrecompiledWeb/WebPagesSample Adding support for vb.net (.vbhtml) Apr 20, 2016
RazorGenerator.Core.Test Run tests as part of build Apr 20, 2016
RazorGenerator.Core.v1 Set priority to Web.config BaseType VS default value May 17, 2016
RazorGenerator.Core.v2 Run tests as part of build Apr 20, 2016
RazorGenerator.Core.v3 Updated tooling dependencies on System.Web.* to latest versions (#148) ( Jun 7, 2017
RazorGenerator.Core Run tests as part of build Apr 20, 2016
RazorGenerator.MsBuild Added RazorSrcFiles as a visual studio available item, allowing razor… Jan 15, 2017
RazorGenerator.Mvc Adding support for vb.net (.vbhtml) to CompositePrecompiledMvcEngine Feb 2, 2017
RazorGenerator.Powershell Fix/issue121 (#139) Mar 9, 2017
RazorGenerator.SingleFileGenerator Normalize all the line endings Jan 20, 2016
RazorGenerator.Templating Update licenseUrl/projectUrl in nuspec. Fixes #112 Aug 11, 2016
RazorGenerator.Testing Update licenseUrl/projectUrl in nuspec. Fixes #112 Aug 11, 2016
RazorGenerator.Tooling Fix/issue121 (#139) Mar 9, 2017
samples Normalize all the line endings Jan 20, 2016
.gitattributes Fix line endings and add .gitattributes Dec 14, 2015
.gitignore Adding test to verify MsBuild package Mar 30, 2015
CommonAssemblyInfo.cs Updated tooling dependencies on System.Web.* to latest versions (#148) ( Jun 7, 2017
LICENSE.txt Add missing license file. Fixes #74 Jan 29, 2016
Local.testsettings Normalize all the line endings Jan 20, 2016
README.md Update README.md May 20, 2016
RazorGenerator.Runtime.sln Normalize all the line endings Jan 20, 2016
RazorGenerator.Runtime.vsmdi Normalize all the line endings Jan 20, 2016
RazorGenerator.Tooling.sln Normalize all the line endings Jan 20, 2016
RazorGenerator.Tooling.vsmdi Normalize all the line endings Jan 20, 2016
TraceAndTestImpact.testsettings Normalize all the line endings Jan 20, 2016
build.ps1 Run tests as part of build Apr 20, 2016
test-msbuild.ps1 Adding test to verify MsBuild package Mar 30, 2015

README.md

RazorGenerator

RazorGenerator.Mvc: NuGet Status

RazorGenerator.MsBuild: NuGet Status

RazorGenerator.Testing: NuGet Status

RazorGenerator.Templating: NuGet Status

TeamCity Build Status: Build status

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

  • MvcHelper: Creates a static type that is best suited for writing Mvc specific helper methods.
  • MvcView: Create a WebViewPage which allows the use of precompiled MVC views.
  • WebPage: Creates a WebPage type that can be used as WebPages Application Part (such as _Admin and RazorDebugger).
  • WebPagesHelper: Creates a HelperPage type that is suited for precompiling and distributing WebPages helper.
  • Template: Generator based on T4 preprocessed template.

Usage in an MVC app

  • Install the 'RazorGenerator.Mvc' package, which registers a special view engine
  • Go to an MVC Razor view's property and set the Custom Tool to RazorGenerator
  • Optionally specify a value for Custom Tool Namespace to specify a namespace for the generated file. The project namespace is used by default.
  • Optionally specify one of the generators in the first line of your Razor file. A generator declaration line looks like this: @* Generator: MvcHelper *@. If you don't specify this, a generator is picked based on convention (e.g. files under Views are treated as MvcViews) NOTE: The selection has other criteria such as detecting "Helper" in the filename and choosing WebPagesHelper for those.
  • You'll see a generated .cs file under the .cshtml file, which will be used at runtime instead of the .cshtml file
  • You can also go to the nuget Package Manager Console and run Enable-RazorGenerator to enable the Custom Tool on all the views.
  • And to cause all the views to be regenerated, go to the nuget Package Manager Console and run Redo-RazorGenerator. This is useful when you update the generator package and it needs to generate different code.

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 into a Razor comment at the top of a cshtml file. Multiple directives are simply whitespace-separated within the comment:

@* Generator: Template  GeneratePrettyNames : true *@

You can also have them on separate lines for clarity but must remain part of the same comment, e.g:

@* Generator: Template  
   GeneratePrettyNames : true *@

Generator type

@* Generator: MvcHelper *@

See above for list of valid values.

disabling line pragmas

@* DisableLinePragmas: true *@

Changing type visiblity

@* TypeVisibility: internal *@

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 *@

Adding generic type parameters

@* Generator: Template GenericParameters: TKey, TValue *@

Add ExcludeForCodeCoverageAttribute to generated files

@* ExcludeForCodeCoverage : true *@

Import additional namespaces during view precompilation

@* Imports: Microsoft.Web.Mvc, MvcContrib, This.WebSite, This.WebSite.Html, HtmlHelpers.BeginCollectionItem *@

Set BaseType during view precompilation

@* BaseType: This.ViewPageBase *@

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

8/26/2/2015 VSIX 1.9

3/19/2015 RazorGenerator.Templating 2.3.3

Add PCL support to RazorGenerator.Templating.

https://github.com/RazorGenerator/RazorGenerator/pull/1

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

  • Added MVC5/Razor3 support

8/28/2013 VSIX 1.5.6

  • Added VS 2013 support + added license file to VSIX

8/22/2013 RazorGenerator.Mvc v2.1.2

6/19/2013 RazorGenerator.Mvc v2.1.1

  • Fix MapPath issue

6/5/2013 Runtime v2.1.0

5/4/2013 Runtime v2.0.1

4/25/2013

10/14/2012

  • Version 1.5 of the VSIX adds support for Razor 2.0 syntax. It auto detects whether it should use 1.0 or 2.0 based on the assembly being referenced by the project.

4/20/2012

  • Version 1.4 of RazorGenerator.Mvc issue with multiple registered view engines (http://razorgenerator.codeplex.com/workitem/55), adds support for overriding the layout page when returning ViewResults and fixes a bug in UsePhysicalViewsIfNewer

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)

  • The generator now finds imported namespaces defined in web.config
  • Add support for project level directives in a razorgenerator.directives file

1.0.1 (6/21/2011)

  • Fix generated line pragmas

1.0

  • Original release

Related blog posts: