当使用"AddControllersWithViews()"时,禁用开发的预编译视图. [英] Disable precompiled views on development when using "AddControllersWithViews()"
问题描述
我正在开发ASP.NET Core 5.0.1项目,我想知道在开发时如何禁用预编译视图.如果我更改了一些html代码,但我不希望这样做,则需要很长的时间才能重新加载页面.我知道它对产品很有用,但我想在开发中禁用它.
I am developing ASP.NET Core 5.0.1 project and I want to know how can I disable precompiled views while I developing. It takes long time to reload page if I change some html code and I don't want that. I know it is useful at product but I want to disable on development.
有人可以帮助我吗?
更新
app.csproj
app.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
Startup.cs
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services, IWebHostEnvironment env)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(routes =>
{
routes.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
}
}
推荐答案
最后我找到了答案.在 ASP.NET Core 3.1 和 ASP.NET Core 5.0 上使用 AddControllersWithViews()时,以下解决方案不起作用:
Finally I've found the answer. When using AddControllersWithViews() on ASP.NET Core 3.1 and ASP.NET Core 5.0, the following solutions are not working:
解决方案比我想象的要简单.
如下更新 .csproj文件,没有任何条件:
Update .csproj file as follow, without any condition:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.1" />
</ItemGroup>
</Project>
更新 ConfigureServices 方法如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddRazorRuntimeCompilation();
}
完成.调试时,程序将不等待编译视图文件.
It's done. When debug, program will not wait to compile view files.
这篇关于当使用"AddControllersWithViews()"时,禁用开发的预编译视图.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!