4

I have cshtml files in a c# web forms project that I want to publish using a publish profile that has the options:

  • Allow precompiled site to be updateable = false

I am using Postal outside of ASP.net http://aboutcode.net/postal/outside-aspnet.html as the emails that are being sent are from a background process. This is using hangfire and is very similar to this: http://docs.hangfire.io/en/latest/tutorials/send-email.html

The problem is that the cshtml file is being precompiled when I dont want it to be and the resulting content of the file is:

This is a marker file generated by the precompilation tool, and should not be deleted!

I need the full contents of the original cshtml file and don't want the marker contents but I do also want to retain the setting of Allow precompiled site to be updateable = false so all other files can not be updated.

The only way I can see to do this is to have Allow precompiled site to be updateable = true

In short I want to deploy the cshtml in the same way that images files are when their build action is set to content.

Any ideas?

Edit: Seems that someone else has the exact same problem:

Is there a way to exclude certain .cshtm files, or an entire folder, from the 'precompile' option so that the .cshtml files can be used outside MVC?

Community
  • 1
  • 1
Steven Anderson
  • 8,398
  • 4
  • 27
  • 32

4 Answers4

3

Currently, there was no way from excluding cshtml (or any other) files from the aspnet_compiler.exe. That means that cshtml will be precompiled along with everything else - there is no way that I could find to get around it.

The way I got this to work was to deploy 'extra' files as part of the publish, rather than have them a part of the web project itself.

This article explains in detail how you can deploy extra files outside of the visual studio project:

ASP.NET Web Deployment using Visual Studio: Deploying Extra Files

In your *.pubxml

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>UAT</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <publishUrl>C:\Publish\</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>False</EnableUpdateable>
    <DebugSymbols>False</DebugSymbols>
    <WDPMergeOption>DonotMerge</WDPMergeOption>
  </PropertyGroup>
  <Target Name="CustomCollectFiles">
    <ItemGroup>
      <_CustomFiles Include="..\ExtraFiles\**\*" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>
  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>    
    <CopyAllFilesToSingleFolderForMsdeployDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForMsdeployDependsOn>
  </PropertyGroup>
</Project>

Place your 'extra files' in a folder called 'ExtraFiles' in the parent directory of the root of your web project and they will be copied over during publish.

Steven Anderson
  • 8,398
  • 4
  • 27
  • 32
1

If you are not using .pubxml, you can add Target after CopyAllFilesToSingleFolderForMsdeploy at the end of your .csproj file. It copies data from any number of sources to package folder, after files are prepared for packaging and before packaging starts:

<Target Name="AdditionalFilesForPackage" AfterTargets="CopyAllFilesToSingleFolderForMsdeploy">
    <Message Text="AdditionalFilesForPackage(): MSBuildProjectDirectory -&gt; $(MSBuildProjectDirectory)" />
    <Message Text="AdditionalFilesForPackage(): _PackageTempDir -&gt; $(_PackageTempDir)" />
    <ItemGroup>
        <FolderFiles Include="$(MSBuildProjectDirectory)\FolderName\**\*" />
        <OtherCshtmlFiles Include="$(MSBuildProjectDirectory)\OtherCshtml\**\*" />
    </ItemGroup>
    <Copy SourceFiles="@(FolderFiles)" DestinationFiles="@(FolderFiles->'$(_PackageTempDir)\FolderName\%(RecursiveDir)%(Filename)%(Extension)')" />
    <Message Text="AdditionalFilesForPackage(): Done copying FolderName files." />
    <Copy SourceFiles="@(OtherCshtmlFiles)" DestinationFiles="@(OtherCshtmlFiles->'$(_PackageTempDir)\OtherCshtml\%(RecursiveDir)%(Filename)%(Extension)')" />
    <Message Text="AdditionalFilesForPackage(): Done copying OtherCshtml files." />
</Target> 
CrnaStena
  • 3,017
  • 5
  • 30
  • 48
0

Views can be kept precompiled when, after publishing, the view files for you emails are overwritten again with the originals again.

I perform this step manually for now, by overwriting the email view files via FTP.

Waaghals
  • 2,029
  • 16
  • 30
0

In my case (I'm not using the RazorViewEngine directly, but loading .cshtml from the file system instead). I changed the .cshtml to .html and it stopped being part of the compilation.

apostolov
  • 1,646
  • 16
  • 20