This walkthrough will address the following questions and teach you how to publish CSHTML files along with the project.
- How to create an Asp.Net Core MVC Project?
- What are the settings for the Project file to publish the CSHTML file while publishing?
- What are the settings to be done on the publish screen or wizards before publishing?
We are using Visual Studio 2022 for this walk-through.

How to create an ASP.NET Core MVC Project?

Select ASP.NET Core web App (Model-View-Controller as per IMAGE1.

Set the following things in the above image 2:
- Project name: Set your project name: AspNetMvcNet8Proj.
- Location: Set your location by clicking… or select previous by clicking on the dropdown list.

- Framework: Select your .NET version (6.0, 8.0. . .)
We selected .NET 8
- Authentication Type: None
- (You can select as per your requirement – None, Individual Accounts, Microsoft Identity Platform, or Windows)
- Configure for HTTPS: Set Configure for HTTPS for secure request and response.
- Enable Container Support: Select Docker for Windows or Linux operating system.
After the above selection, choose as per your project requirement. Click on the CREATE button to proceed toward creating a new project.
Press F5 to run the project.

You can see the above project executed successfully.
The next step is to publish the project with a CSHTML file by default while publishing all views files compiled and output into one DLL (assembly). No external CSHTML published.
What are the settings for the Project file to publish the CSHTML file while publishing?

Right-click on the project
Select “Unload Project”
Default Content:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Change the file to the following settings:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PreserveCompilationContext>true</PreserveCompilationContext>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
<!-- To publish views instead of views.dll -->
<EnableDefaultRazorGenerateItems>false</EnableDefaultRazorGenerateItems>
<EnableDefaultRazorComponentItems>false</EnableDefaultRazorComponentItems>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0" />
<!-- Explicitly include each .cshtml file in the subfolders under Views -->
<Content Update="Views\**\*.cshtml" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
</Project>
After updating the above settings in the file, close the file again, right-click on the project, and click on “Reload Project”. In the Next step, we set and configure Publish settings.
What are the settings to be done on the publish screen or wizards before publishing?

After Right click on the project, select the “Publish” option.

Select the FOLDER option to publish on the Local Folder and click on the NEXT button.

Select or create a folder where you want to publish the project. Click on the FINISH button after you select the folder location.

Click on Show All settings to set your publish setting more precisely, or directly click on the PUBLISH button. Output of Publish Button:

Error Message: Views\Shared\_Layout.cshtml.css(0,0): Error BLAZOR102: The scoped css file 'Views\Shared\_Layout.cshtml.css' was defined, but no associated razor component or view was found for it.

You can see the error thrown by the publishing process in (Image-10 and Image-11).
Error Solution
Solution 1
You can delete the CSS file “View/Shared/Layout.cshtml” and publish it again.
Solution 2
If you want CSS style or content of the CSS file “View/Shared/Layout.cshtml,” you can create a new CSS file and move this file into the “wwwroot/css” folder and assign the link in the “view/shared/Layout.cshtml” file.

VIEW published successfully
Folder View


You can see in Image-13 and Image-14 Views files were published successfully.