当前位置:网站首页>be based on. NETCORE development blog project starblog - (12) razor page dynamic compilation
be based on. NETCORE development blog project starblog - (12) razor page dynamic compilation
2022-06-22 03:47:00 【Dotnet cross platform】
Series articles
be based on .NetCore Developing blog projects StarBlog - (3) Model design
be based on .NetCore Developing blog projects StarBlog - (4) markdown Blog batch import
be based on .NetCore Developing blog projects StarBlog - (5) Begin to build Web project
be based on .NetCore Developing blog projects StarBlog - (6) List of blog posts for page development
be based on .NetCore Developing blog projects StarBlog - (7) Page development article details page
be based on .NetCore Developing blog projects StarBlog - (8) The classification hierarchy shows
be based on .NetCore Developing blog projects StarBlog - (9) Batch import of pictures
be based on .NetCore Developing blog projects StarBlog - (10) Picture waterfall flow
be based on .NetCore Developing blog projects StarBlog - (11) Implement access statistics
be based on .NetCore Developing blog projects StarBlog - (12) Razor Page dynamic compilation
...
Preface
I haven't updated my blog development notes for some time , Lazy = =..
Take advantage of the weekend , To improve the blog project some details of the function , Then prepare to deploy and go online ~
Originally, this article was to record several functions ( Topic switching 、 Project monitoring 、 Random picture interface ) Implementation of , But when I was writing the page, I found that every time I changed it Razor It needs to be restarted. It's a big trouble , So I stepped on the pit , Then there is this article .
Razor Page dynamic compilation
although .Net6 Some code hot updates are supported , But it's still chicken ribs , Change it all the time Razor Page, but click Apply It still doesn't work ~
I'll use it again .NetCore3.1 The previously used dynamic compilation is better ~
First NuGet install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation, stay Program.cs Middle configuration
Original addition MVC The service is like this
builder.Services.AddControllersWithViews(
options => { options.Filters.Add<ResponseWrapperFilter>(); }
)To such ( Development mode enables dynamic compilation )
var mvcBuilder = builder.Services.AddControllersWithViews(
options => { options.Filters.Add<ResponseWrapperFilter>(); }
);
if (builder.Environment.IsDevelopment()) {
mvcBuilder.AddRazorRuntimeCompilation();
} It can also be modified without modification C# Code , By means of environment variables , modify launchSettings.json file , Add an environment variable
In this way, the program will be loaded dynamically when it is started RuntimeCompilation.
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}in addition , There are more radical ways , Don't compile when publishing and packaging cshtml file , Easy to modify after publishing Razor page .
In the project profile (xxx.csproj) Middle configuration
<PropertyGroup>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>So when it is released , be-all Razor Views are not precompiled , And all views will be published together , Convenient for online environment modification Razor page .
Note, however, that in the production environment of the release , Modifying the view file is Will not take effect immediately , Need to restart the program ( about IIS The running environment of the host needs to restart the site ) Will take effect .
You can also configure conditional compilation , For details, please refer to :https://www.cnblogs.com/thinksea/articles/14772837.html
This is the end of the modification Razor page , preservation , Will automatically recompile , It is convenient to view the effect in real time after modifying the page .
Configure the error after dynamic compilation
I have a problem here , Before .NetCore3.1 I haven't encountered any
Added the error report of the startup program after dynamic compilation
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)
... Omit a lot of error messages
at Program.<Main>$(String[] args) in StarBlog\StarBlog.Web\Program.cs:line 96After searching , Find one 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.
Take a look at mine dotnet SDK edition , yes 6.0.101 , Seems to be SDK Of bug, Just update the version ~
( After ten minutes ) Updated , Sure enough, the problem was solved !
Reference material
.Net Core 5.0 Razor precompile , Dynamic compilation , 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
边栏推荐
- replacement has 2 rows, data has 0, 解决R语言如何动态生成dataframe
- Mysql 45讲学习笔记(二)SQL更新语句的执行
- 逆向crackme之ESp定律脱壳
- svn与cvs的区别有哪些
- 八大排序之直接插入排序
- 3DE recover from design
- Flutter-渲染原理&三棵树详解
- Ads communication between Beifu twincat3 controller and controller
- TwinCAT 3 RS232通信的关键程序
- Official competition volume and "answer" of "Cyberspace Security" of secondary vocational group in 2019 national vocational college skills competition
猜你喜欢

Beifu twincat3 ads error query list

3DE new simulation status

云原生架构(02)-什么是云原生

Flutter-渲染原理&三棵树详解

How to synchronize the oak camera?

How to do activities in beauty salons

Based on logback XML to realize the insensitive operation of saving log information

Wechat applet ChAT expression

The following assertion was thrown during performlayout

PLC program variable definition and hardware IO Association in Bifu twincat3
随机推荐
未来已来:云原生时代
How to read and write files efficiently
【Leetcode】17回溯(电话号码的字母组合)
Attributes, comments and field information of fields in MySQL query table
MySQL 45 lecture notes (I) execution of an SQL statement
Processes is not enough. Users can't log in
逆向crackme之ESp定律脱壳
利用jemalloc解决flink的内存溢出问题
Use yolov5 to train your own data set; Installation and use of yolov5; Interpretation of yolov5 source code
面试官:知道 Flutter 生命周期吗?
Blazor University (31)表单 —— 验证
IIR滤波器设计基础及Matlab设计示例
Flutter-Stream使用
How to synchronize the oak camera?
倍福TwinCAT设置PLC的扫描周期,运行周期方法
1690. stone game vii- dynamic programming method
2022.6.21-----leetcode. one thousand one hundred and eight
ORA-32700: error occurred in DIAG Group Service
The following assertion was thrown during performlayout
IIR filter design basis and MATLAB design example