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!
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”).
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.
- Install the 'RazorGenerator.Mvc' package, which registers a special view engine
- Go to an MVC Razor view's property and set the
Custom Tool
toRazorGenerator
- 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 asMvcViews
) NOTE: The selection has other criteria such as detecting "Helper" in the filename and choosingWebPagesHelper
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.
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.
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: MvcHelper *@
See above for list of valid values.
@* DisableLinePragmas: true *@
@* TypeVisibility: internal *@
@* GenerateAbsolutePathLinePragmas: true *@
@* Namespace: SomeNamespace *@
@* ClassSuffix: _ *@
@* GeneratePrettyNames : true *@
@* TrimLeadingUnderscores : true *@
@* Generator: Template GenericParameters: TKey, TValue *@
@* ExcludeForCodeCoverage : true *@
@* Imports: Microsoft.Web.Mvc, MvcContrib, This.WebSite, This.WebSite.Html, HtmlHelpers.BeginCollectionItem *@
@* 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
- Add Visual Studio 2019 support.
- Add generic type parameters directive. RazorGenerator#34
Add PCL support to RazorGenerator.Templating.
- Improve MVC version detection logic. https://razorgenerator.codeplex.com/discussions/459467
- Fix EditorTemplate issue https://razorgenerator.codeplex.com/workitem/133
- Fix MVC5 detection logic https://razorgenerator.codeplex.com/discussions/459467
- Fix issue 132: crash when two directives conflict https://razorgenerator.codeplex.com/workitem/132
- Fix more ~/ path logic https://razorgenerator.codeplex.com/SourceControl/network/forks/beelineuk/RazorGeneraorForSitecore/contribution/5619
- Added MVC5/Razor3 support
- Added VS 2013 support + added license file to VSIX
- Fix issue #124: bad ~/ path logic https://razorgenerator.codeplex.com/workitem/124
- Fix MapPath issue
- New CompositePrecompiledMvcEngine https://razorgenerator.codeplex.com/SourceControl/network/forks/odinserj/RazorGenerator/contribution/4780
- Fixed regression https://razorgenerator.codeplex.com/workitem/107
- v2.0: All the runtime assemblies are now signed.
- View activation process unification https://razorgenerator.codeplex.com/workitem/105
- 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.
- 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
- Version 1.2.3 of the VSIX fixes MVC4 issue (http://razorgenerator.codeplex.com/workitem/26)
- Version 1.3.1 of RazorGenerator.Testing fixes an issue with RenderAsHtml not flowing the context
- Renamed all packages to be RazorGenerator.*
- Fix issue http://razorgenerator.codeplex.com/workitem/22 (Line pragmas should not use absolute paths)
- Fixed encoding issue when using ANSI (non UTF-8) razor files (http://razorgenerator.codeplex.com/workitem/15)
- Support null values in simple template scenario. e.g. @foo when foo is null now renders empty string. (http://razorgenerator.codeplex.com/workitem/16)
- Change the generated file extensions from foo.cs to foo.generated.cs (http://razorgenerator.codeplex.com/workitem/8)
- The generator now finds imported namespaces defined in web.config
- Add support for project level directives in a razorgenerator.directives file
- Fix generated line pragmas
- Original release