Home > Software design >  how to disable precompile MVC cshml page ASP.NET Core 5.0
how to disable precompile MVC cshml page ASP.NET Core 5.0

Time:09-17

I want to avoid precompile of my MVC .cshtml files

During publishing ASP.NET Core 5.0 website (not Razor Views) it is pre-compile my MVC .cshtml views.

Instead of dll, I want normal .cshml file

I have tried following ways but it is not working.

Modified project file

    <PropertyGroup>
      <TargetFramework>net5.0</TargetFramework>
      <PreserveCompilationContext>true</PreserveCompilationContext>
      <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
      <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
    </PropertyGroup>
    
    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.1" />
    </ItemGroup>

</Project>

Modified startup.cs html

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

Can anyone please help me to figure it out this problem?

CodePudding user response:

According to this Github issue, the .csproj flag is now called <RazorCompileOnPublish> for ASP.NET Core 3 and above:

the property MvcRazorCompileOnPublish is supported for legacy purposes [...], but we recommend using <RazorCompileOnPublish>false</RazorCompileOnPublish> instead.

CodePudding user response:

just reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation in your .csproj

  <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.10" />

And add these two lines of code

  <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

Eventually your cs file will look something like this :

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
 <TargetFramework>net5.0</TargetFramework>
  <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
 <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
 <ItemGroup>
 <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.10" />
 </ItemGroup>
</Project>

and in your Startup File Add Service Like This

 services.AddControllersWithViews().AddRazorRuntimeCompilation(); 

and After publishing it, you can see the views and pages folders in your files and edit them.

  • Related