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博客开发笔记(4):markdown博客批量导入
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetCore开发博客项目 StarBlog - (3) 模型设计 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入 ... 前言 上周介绍了博客的模型设计,现在模
2025-04-27 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记:部署
Nginx 反向代理 后端配置中间件 app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); Nginx配置 server { listen
2022-12-05 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记(3):模型设计
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetCore开发博客项目 StarBlog - (3) 模型设计 ... 基于.NetCore开发博客项目 StarBlog - 接口返回值包装 基于.NetCore开发博客项目 StarBlog - 上传
2022-04-22 · StarBlog博客开发笔记 · 约 1 分钟
StarBlog博客开发笔记:接口返回值包装
我的做法: 自己定义一套返回值包装对象 用过滤器处理状态码 用过滤器包装错误信息 参考资料 【5min+】美化API,包装AspNetCore的返回结果:https://www.cnblogs.com/uoyo/p/12895627.html HTTP 状态码:https://www.runoob.com/http/http-status-codes.html Filters in ASP.NET
2022-02-28 · StarBlog博客开发笔记 · 约 1 分钟
讨论
共 0 条加载评论中…