In this walk-through, you will learn how to publish CSHTML files while publishing the project, and you will get answers to the following questions.
- 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?
![Create a new project]()
Image-1
Select ASP.NET Core web App (Model-View-Controller as per IMAGE1.
![Configure your new project]()
Image 2
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.
![Additional information]()
Image 3
- 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.
![Welcome]()
Image 4
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?
![Solution explorer]()
Image 5
Right-click on the project “AspNetMvcNet8Proj”
- Select “Unload Project”
- Default Content of “AspNetMvcNet8Proj.csproj” file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Change the “AspNetMvcNet8Proj.csproj” 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 “AspNetMvcNet8Proj.csproj” 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?
![Publish]()
Image 6
After Right click on the project, select the “Publish” option.
![Folder]()
Image 7
Select the FOLDER option to publish on the Local Folder and click on the NEXT button.
![Publish]()
Image 8
Select or create a folder where you want to publish the project.
Click on the FINISH button after you select the folder location.
![Show all settings]()
Image 9
Click on Show All settings to set your publish setting more precisely, or directly click on the PUBLISH button.
Output of Publish Button:
![Output of publish button]()
Image 10
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.
![Output]()
Image 11
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.
![How-To-Publish-CSHTML-in-AspNet-Core-MVC-Net8-Project]()
Image 12
VIEW published successfully
Folder View
![How-To-Publish-CSHTML-in-AspNet-Core-MVC-Net8-Project]()
Image 13
![How-To-Publish-CSHTML-in-AspNet-Core-MVC-Net8-Project]()
Image 14
You can see in Image-13 and Image-14 Views files were published successfully.
Happy Coding…!