Disable precompiled views on development when using “AddControllersWithViews()”

空扰寡人 提交于 2021-01-27 16:32:23

问题


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.

Anyone can help me?

UPDATE

app.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>

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?}"
            );
        });
    }
}

回答1:


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:

  • Official Document
  • StackOverFlow Solution

The solution is simple than I think actually.

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>

Update ConfigureServices method as follow:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews().AddRazorRuntimeCompilation();
}

It's done. When debug, program will not wait to compile view files.



来源:https://stackoverflow.com/questions/65768589/disable-precompiled-views-on-development-when-using-addcontrollerswithviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!