在Vs2022下,如何在开发调试时修改了cshtml,不重新启动就能马上生效呢?
1.要用NutGet下载并安装Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 6.0.1
2.修改Properties\launchSettings.json文件,
"profiles": { "AspNETCore_MVC": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" }, "applicationUrl": "http://localhost:5099", "dotnetRunMessages": true }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "http://localhost:22473/T1/Question", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" }
这两个地方都加,这样不管是用哪种容器,都可以生效。
至于startup里的services.AddControllersWithViews().AddRazorRuntimeCompilation(),是不用加的(加了也没用)
参考:Razor文件编译 ASP.NET Core | Microsoft Docs
中【有条件地启用现有项目中的运行时编译】一节。
好了,这样启动了项目后,可以直接在Vs里修改一个cshtml文件,再刷新浏览器,就可以看到刷新了。