One of my main ambitions for DasBlog Core was to create a flexible templating solution using Razor, this would allow a consumer of the platform to quickly make wholesale edits to the layout of their blog, and of course this would be possible without requiring a wholesale rebuild. This seemed straightforward at first, mostly because I was thinking about Razor pages as structurally similar to aspx pages, and this by default, is definitely not the case.
As I have stated previously the intent was to create a "theme" folder and under that theme folder would a be predefined set of themes. The structure would look something like \theme\fulcrum or \theme\median, and after building the application the expectation was that these folders would contain a list of the partial views that each theme needed.
What I immediately noticed is that after building and deploying my MVC application the razor pages contained in my themes folder would disappear, well not disappear exactly, they were getting compiled into an assembly leaving me with no opportunity to make modifications after the build and deploy step. This would mean if someone wanted to modify a theme it would force a complete recompile, and this would simply not do.
Under the hood I found out that the rendering phase was all happening at compile time, a comprehensive post by David Glick that broke down those rendering steps as follows:
Turning Razor content from a string, file, or other source into final rendered HTML requires several phases:
- Generating C# code from the template
- Compiling the C# code into an assembly
- Loading the assembly into memory
- Executing your compiled template
What this meant for me was that we are not actually dealing with a string file when we deploy our blogging framework we are actually executing a compiled assembly. This was leaving no simple opportunity to modify a string based file after compilation and deployment.
Using Razor SDK
There is some hope, we could actually use the Razor SDK to redefine the build compile process of the Razor pages. It requires the installation of the latest Microsoft.AspNetCore.Razor.Design nuget package.
After installing the package you can add the following properties to your web project:
Now when you look at your deployed project you will find all your razor views have been distributed and get compiled during app startup, so if you need to make a change to razor file you will need to do a soft restart of the web app. As of this writing I have created about three themes, however, I am looking for more help and I think the process will be easier now that the lastest Visual Studio Code preview supports Razor.
Comments are closed.