如何在发布期间从预编译中排除cshtml



我在一个c#web表单项目中有cshtml文件,我想使用具有以下选项的发布配置文件来发布:

  • 允许预编译网站可更新=false

我在ASP.net之外使用Postalhttp://aboutcode.net/postal/outside-aspnet.html因为正在发送的电子邮件来自后台进程。这是在使用hangfire,与此非常相似:http://docs.hangfire.io/en/latest/tutorials/send-email.html

问题是cshtml文件是在我不希望的时候预编译的,文件的结果内容是:

这是由预编译工具生成的标记文件,不应删除!

我需要原始cshtml文件的全部内容,不需要标记内容,但我也想保留Allow precompiled site to be updateable = false的设置,这样所有其他文件都无法更新。

我能看到的唯一方法是使用Allow precompiled site to be updateable = true

简而言之,当图像文件的构建操作设置为内容时,我希望以与图像文件相同的方式部署cshtml。

有什么想法吗?

编辑:似乎其他人也有同样的问题:

有没有办法从';预编译';选项,以便.cs.html文件可以在MVC之外使用?

目前,没有办法从aspnet_compiler.exe中排除cshtml(或任何其他)文件。这意味着cshtml将与其他所有文件一起预编译-我找不到绕过它的方法。

我实现这一点的方法是将"额外"文件作为发布的一部分进行部署,而不是将它们作为web项目本身的一部分。

本文详细解释了如何在visualstudio项目之外部署额外的文件:

使用Visual Studio进行ASP.NET Web部署:部署额外文件

在*.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>

将您的"额外文件"放在web项目根目录的父目录中名为"ExtraFiles"的文件夹中,它们将在发布过程中被复制。

如果不使用.pubxml,可以在.csproj文件末尾的CopyAllFilesToSingleFolderForMsdeploy之后添加Target。在准备好文件进行打包之后,在打包开始之前,它将来自任意数量来源的数据复制到打包文件夹中:

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

当发布后,您的电子邮件的视图文件再次被原始文件覆盖时,视图可以保持预编译状态。

我现在手动执行这一步骤,通过FTP覆盖电子邮件视图文件。

在我的情况下(我不是直接使用RazorViewEngine,而是从文件系统加载.cs.html)。我将.cshtml更改为.html,它不再是编译的一部分。

最新更新