当前位置:网站首页>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
边栏推荐
- 未來已來:雲原生時代
- Implementation of common function blocks for Beifu twincat3 servo control
- IIR filter design basis and MATLAB design example
- Talk about the Flink waterline
- Key program of TwinCAT 3 RS232 communication
- H指数问题
- 1299. 将每个元素替换为右侧最大元素
- 云原生架构(02)-什么是云原生
- Zombie process and orphan process
- [leetcode] 17 backtracking (letter combination of telephone number)
猜你喜欢

Using jemalloc to solve the memory overflow problem of Flink

L'avenir est venu: l'âge du nuage

Wireshark数据包分析——wireshark0051.pcap

IIR滤波器设计基础及Matlab设计示例

多线程 interrupt用法

What are the differences between SVN and CVS

How to do activities in beauty salons

倍福TwinCAT设置PLC的扫描周期,运行周期方法

倍福TwinCAT3控制器和控制器间的Ads通讯

Flyter color gradient and follow button imitating Taobao gradient
随机推荐
Talk about the Flink waterline
C language integer value range - the problem of more than one negative number
The future has come: cloud primary Era
Based on logback XML to realize the insensitive operation of saving log information
嵌入式软件测试的经验经历和总结
3000 yuan projector comparison and evaluation, dangbei D3x beats Jimi new Z6 x
Comment dart asynchrone est implémenté
Processes is not enough. Users can't log in
详细专业的软件功能测试报告应该怎样书写
ORA-32700: error occurred in DIAG Group Service
MySQL 45 lecture learning notes (II) execution of SQL update statements
所有项目的资源
76. 最小覆盖子串-滑动窗口法
倍福TwinCAT设置PLC的扫描周期,运行周期方法
Rabbmitmq publishing keyword mode < 3 >
Hierarchical traversal of binary tree
Flutter-状态管理
3DE save to Favorites
LOCAL=NO
Flutter-Stream使用