aspnetcore: Unable to publish views of a Microsoft.NET.Sdk.Web project

Please let me know the steps I need to follow to publish views (.cshtml files) of a Microsoft.NET.Sdk.Web project instead of publishing a precompiled views.dll I am using asp.net core 5.0, and please note it is not a Microsoft.NET.Sdk.Razor project.

  1. Tried the following settings but that didn’t help. Do these work only for Microsoft.NET.Sdk.Razor projects? If yes, then what are corresponding settings in a Microsoft.NET.Sdk.Web project for the following:
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<RazorCompileOnPublish>false</RazorCompileOnPublish>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
  1. Also tried include Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package and including the AddRazorRuntimeCompilation():
services.AddMvc(options => options.Filters.Add(typeof(CustomExceptionFilter)))
                .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null)
                .AddControllersAsServices()
                .AddRazorRuntimeCompilation();

These didn’t help. So please let me know what I need to do be able to publish views in a Microsoft.NET.Sdk.Web project .

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

I think putting <Content Update="*.cshtml" CopyToPublishDirectory="PreserveNewest" /> after all your includes might work. That said this is a question specific to MSBuild evaluation, and perhaps folks on https://github.com/dotnet/msbuild or StackOverflow might be better suited to respond.

CopyRazorGenerateFilesToPublishDirectory applies to RazorGenerate items which are only calculated whenEnableDefaultItems is true.

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

You’ll have to manually include cshtml files as Content items:

e.g. <Content Include="**\*.cshtml" /> (this does not include any of the the excludes that the Web.SDK adds by default).

@geetmenon thanks for contacting us.

Microsoft.NET.Sdk.Web imports Microsoft.NET.Sdk.Razor, otherwise you wouldn’t even be able to compile builds.

I’m not sure what you are trying to achieve, the best way for you to do this would probably be to set EnableDefaultRazorGenerateItems and EnableDefaultRazorComponentItems

At that point they won’t be considered for razor compilation and won’t be compiled nor included in the dlls and is up to you to get them copied to wherever you want.

/cc: @pranavkm