Building String Razor Template Engine with Bare Hands NET Core
When you create a custom view engine, you might often want to remove the default view engines. You can remove both the Razor View Engine and the ASPX View Engine and then add your own custom view engine as shown in the code snippet given below. You can also create your own custom view engine in ASP.NET Core.
All we’re doing here is saying “let’s create an empty file system and create a default empty project”. The razorTemplateBaseFolder constant is used to define the name of the folder, in the host project, where we expect to find the .cshtml template file. The only difference in this project is that we just need to manually make sure that it gets copied in the build.
Building String Razor Template Engine with Bare Hands (.NET Core)
Space is not allowed in the Code expression, as it is used to identify the end of the expression. The Razor View engine evaluates the expressions, and the result is inserted in their place. In this post I have showed how you can use RazorEngineCore as template engine outside of ASP.NET MVC. In production environment you may want to add more stuff to it (eg. caching), but the purpose of this article was to show that it exists and is easy to use. I find it useful specially in combination with Playwright.NET. To make our Razor Engine strongly typed we need to create a CompiledTemplate class with just one Run method and an extension method which will compile the template for us.
- The Razor view engine has become popular for developers working with ASP.NET Core MVC due to its intuitive syntax and powerful features for creating dynamic web content.
- At the end of this article, you will understand the following pointers.
- This package uses .NET Standard 2, so it should work for .NET Core applications targeting the 2+ and 3+ runtimes.
- Here is a solution that is encapsulated for usage outside of an mvc app.
- The act of bringing Roslyn into the runtime and the actual compilation step itself is computationally expensive, so you want to avoid repeating this work as much as possible.
- There is no MVC in that piece of code, so, no View, no .cshtml files, no Controller, just Razor source parsing and compiled runtime execution.
The main interface to provide RazorEngine with templates is the ITemplateManager interface. You should either pick one of the available implementations or write your own. We’ll use this project to work with Razor views in the subsequent sections of this article. Our results are exactly like the previous sample, but now we’re using our Person class to strongly-type our template. Any content within the () parenthesis is evaluated and rendered to the output.
The Generic Base Template Model
In HtmlSafeTemplate class we override methods responsible for writing content from our properties and call HTMLEncode against them, so they become HTML safe. Additionally Raw method is added, which razor engine .net core mimics behavior of MVC’s HTML.Raw. You could omit and add properties for your model in RazorTemplate class. Within the source folder I will create a new Class Library project – HTML2PDF.RazorEngine.
The SyntaxTree object is returned as output from this method and this will be used in the actual compilation which happens in a subsequent step. Although objects with names such as ProjectFileSystem appear to sound like we’re going to be reading our template file from disk, this is not what is happening at this stage. Both parts of the template are expected to live in the project (assembly) that is calling the engine – not the RazorEngine project directly.
Build and test the project
The important thing to note here is that, contrary to what you may be more used to, we’re not telling the View to directly use the ViewModel. Instead, we’re adding a reference to a base-model (which we’ll talk about shortly). The ViewModel is still part of this picture though, as it is specified as a generic argument of the base model. Defining that a file should be copied in the build is easy to do, but also easy to forget. When you come to work on your own projects, if you’re having problems, make sure to check that you’ve performed this step. By hard-coded convention, the demo will expect you to put the files in a root folder of the host project called EmailTemplates.