Skip to content

Commit abbce00

Browse files
committed
Added readme
1 parent d574835 commit abbce00

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

MSBuildRazorCompiler.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1414
.gitattributes = .gitattributes
1515
.gitignore = .gitignore
1616
build.ps1 = build.ps1
17+
README.md = README.md
1718
EndProjectSection
1819
EndProject
1920
Global

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# MSBuild Razor Compiler
2+
3+
This library provides a .NET Core 3.1 compatible MSBuild task that will automatically compile all `.cshtml` files into `.cs` files in-place and also provides you code you can call to then invoke those classes with a model and get the resulting output.
4+
5+
This library is inspired by [RazorGenerator](https://github.com/RazorGenerator/RazorGenerator), which was a custom tool for Visual Studio that created a compiled `.cs` file when saving a `.cshtml` file in your IDE. This was handy for situations where you want to have static template files (e.g. email templates, [library templates](https://github.com/MRCollective/ChameleonForms/tree/master/ChameleonForms/Templates/Default), etc.). RazorGenerator doesn't support .NET Core, hence creating this.
6+
7+
This library is implemented by providing a thin wrapper over the [RazorLight](https://github.com/toddams/RazorLight) library and breaking it up into two stages:
8+
9+
1. Compilation of `.cshtml` files to `.cs` files - this happens just before the `CoreCompile` task of the project you install this library into via the included MSBuild task.
10+
2. Rendering / invocation of the resultant `.cs` code
11+
12+
Because this library generates `.cs` files you don't need to worry about compilation performance, runtime compilation errors or caching or anything like that. It also allows you to have predictability of having real classes you can reference from your code rahter rather than relying on magic strings to find your `.cshtml` files and needing to either ship your `.cshtml` files with your code or embed them into your dll. Using this solution those files are only used at compile time and can then be discarded.
13+
14+
## Getting started
15+
16+
1. Include `SomeFile.cshtml` files in your (.netcore3.1) project
17+
2. `Install-Package `MSBuildRazorCompiler` (work-in-progress, not in nuget.org yet)
18+
3. Compile - you should now see `some-file.cshtml.generated.cs` next to it with a class `SomeFile` in the namespace of your project
19+
4. Execute the following code to get a rendered result: `new SomeFile().Render(model, viewBag or null)` - there is also an async variant

0 commit comments

Comments
 (0)