StarBlog Blog Development Notes (12): Razor Page Dynamic Compilation
Series articles Developing a blog project StarBlog based on .NetCore - (1) Why do you need to write your own blog? Developing a blog project StarBlog based on .NetCore - (2) Environment setup and creating the project Developing a blog project StarBlog based on .NetCore - (3) Model design Developing a blog project StarBlog based on .NetCore - (4) Batch import of markdown blogs Developing a blog project StarBl
目录5 个章节
Series Articles
- Developing a blog project StarBlog based on .NetCore - (1) Why do you need to write your own blog?
- Developing a blog project StarBlog based on .NetCore - (2) Environment setup and creating the project
- Developing a blog project StarBlog based on .NetCore - (3) Model design
- Developing a blog project StarBlog based on .NetCore - (4) Batch import of markdown blogs
- Developing a blog project StarBlog based on .NetCore - (5) Starting to build the Web project
- Developing a blog project StarBlog based on .NetCore - (6) Page development: blog post list
- Developing a blog project StarBlog based on .NetCore - (7) Page development: post detail page
- Developing a blog project StarBlog based on .NetCore - (8) Displaying the category hierarchy
- Developing a blog project StarBlog based on .NetCore - (9) Batch import of images
- Developing a blog project StarBlog based on .NetCore - (10) Image waterfall layout
- Developing a blog project StarBlog based on .NetCore - (11) Implementing visit statistics
- Developing a blog project StarBlog based on .NetCore - (12) Razor page dynamic compilation
- ...
Preface
I haven't updated the blog development notes for a while recently—got lazy = =..
Over the weekend, I polished some detailed features of the blog project and got ready to deploy it live~
This article was originally meant to document the implementation of a few features (theme switching, project monitoring, and a random image API), but while writing pages I found it annoying to have to restart every time I changed a Razor page, so I went through some pitfalls, and that's how this article came about.
Razor Page Dynamic Compilation
Although .Net6 started supporting partial code hot reload, it's still pretty half-baked—I often modify Razor pages but clicking Apply still has no effect~
I'd rather go back to the dynamic compilation that was commonly used before .NetCore3.1~
First, install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation via NuGet, then configure it in Program.cs
Originally, adding MVC services looked like this
builder.Services.AddControllersWithViews(
options => { options.Filters.Add<ResponseWrapperFilter>(); }
)
Change it to this (enable dynamic compilation only in development mode)
var mvcBuilder = builder.Services.AddControllersWithViews(
options => { options.Filters.Add<ResponseWrapperFilter>(); }
);
if (builder.Environment.IsDevelopment()) {
mvcBuilder.AddRazorRuntimeCompilation();
}
You can also do it without modifying C# code, via an environment variable. Edit the launchSettings.json file and add an environment variable
This way, RuntimeCompilation will be loaded dynamically when the program starts.
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
There's also a more thorough approach: don't compile cshtml files when publishing, so you can modify Razor pages after deployment.
Configure it in the project file (xxx.csproj)
<PropertyGroup>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
This way, when publishing, all Razor views will not be precompiled, and all views will be published together, making it convenient to modify Razor pages in the production environment.
However, note that in a published production environment, modifying view files will not take effect immediately; you need to restart the application (for IIS-hosted environments, you need to restart the site) for the changes to take effect.
You can also configure conditional compilation. For details, see: https://www.cnblogs.com/thinksea/articles/14772837.html
This way, after you modify a Razor page and save, it will automatically recompile, making it convenient to see the effect in real time after changing the page.
Error After Configuring Dynamic Compilation
I ran into a problem here that I hadn't encountered in previous .NetCore3.1 projects
After adding dynamic compilation, the program threw an error on startup
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Linq.Enumerable.OfType[TResult](IEnumerable source)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionDescriptorProvider..ctor(IEnumerable`1 pageRouteModelProviders, IOptions`1 mvcOptionsAccessor, IOptions`1 pag
esOptionsAccessor)
...省略一大堆报错信息
at Program.<Main>$(String[] args) in StarBlog\StarBlog.Web\Program.cs:line 96
After searching, I found a Github issues: https://github.com/dotnet/aspnetcore/issues/40609
Do you have the 6.0.201 SDK / 6.0.3 Microsoft.AspNetCore.App runtime installed? Patch builds of packages typically require a corresponding version of the runtime to be installed.
I checked my dotnet SDK version—it's 6.0.101. Looks like an SDK bug; updating the version should fix it~
(Ten minutes later) The update is done, and the problem is indeed solved!
References
- .Net Core 5.0 Razor precompilation, dynamic compilation, and mixed compilation: https://www.cnblogs.com/thinksea/articles/14772837.html
- .NET Hot Reload support for ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/test/hot-reload?view=aspnetcore-6.0
- Razor file compilation in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-6.0&tabs=visual-studio
- Can't get RazorPageOptions from DI with version 6.0.3 of Mvc.Razor.RuntimeCompilation: https://github.com/dotnet/aspnetcore/issues/40609
_EOF_
StarBlog Blog Development Notes (12): Razor Page Dynamic Compilation
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
StarBlog博客开发笔记(1):项目介绍
前言 OK,我也来造轮子了 博客系统从一开始用WordPress,再到后来用hexo、hugo之类的静态博客生成放github托管,一直在折腾 折腾是为了更好解决问题,最终还是打算自己花时间搞一个好了 本系列文章将记录博客的开发过程~ 后端基于最新的.Net6和Asp.Net Core框架,遵循RESTFul接口规范,前端基于Vue+ElementUI开发,可作为 .Net Core 入门项目学习
2022-04-05 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记(21):开始开发RESTFul接口
前言 最近电脑坏了,开源项目的进度也受到一些影响 这篇酝酿很久了,作为本系列第二部分(API接口开发)的第一篇,得想一个好的开头,想着想着就鸽了好久,索性不扯那么多了,直接开写吧~ 关于RESTFul 网上很多相关的文章都要把RESTFul历史来龙去脉给复制一遍,所以我这就不重复了,现在主要的HTTP接口风格就俩:RPC和RESTFul。 举个例子就可以看出这俩的区别 RPC风格 分别是增删改查的
2023-01-02 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记(13):加入友情链接功能
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetCore开发博客项目 StarBlog - (3) 模型设计 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入 基于.NetCore开发博客项目 StarBl
2022-06-28 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记(25):图片接口与文件上传
前言 上传文件的接口设计有两种风格,一种是整个项目只设置一个接口用来上传,然后其他需要用到文件的地方,都只存一个引用ID;另一种是每个需要文件的地方单独管理各自的文件。这俩各有优劣吧,本项目中选择的是后者的风格,文章图片和照片模块又要能CRUD又要批量导入,还是各自管理文件比较好。 图片接口 说会正题,先介绍一下图片相关接口。 图片列表 首先CRUD是肯定有的,图片列表的分页查看也是有的,不过因为
2023-02-05 · StarBlog博客开发笔记 · 约 1 分钟
讨论
共 0 条加载评论中…