Search code examples
visual-studiorazorhangfirepostal

How to exclude cshtml from pre compilation during publish


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?


Solution

  • 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.