This happens usually when you are precompiling templates of a big project with many subfolders, or with long name paths.
To fix it, we need to set an intermediate folder to precompile our files before copying them to our publish folder following these steps:
- Go to your project folder, navigate to Properties\PublishProfiles folder.
- Open your profile file “profile_name.pubxml”, ignore the profile_name.pubxml.user.
- Add this param to your profile config with your chosen folder <AspnetCompileMergeIntermediateOutputPath>[PATH_TO_YOUR_FOLDER]</AspnetCompileMergeIntermediateOutputPath> under <PropertyGroup> tag. In my case I chose a folder with a really short name: “D:\.net\”.
- Save your file and you should be able to publish your project.