【问题标题】:How to exclude cshtml from pre compilation during publish如何在发布期间从预编译中排除 cshtml
【发布时间】:2015-03-20 05:52:11
【问题描述】:

我在 c# Web 表单项目中有 cshtml 文件,我想使用具有以下选项的发布配置文件进行发布:

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

我在 ASP.net http://aboutcode.net/postal/outside-aspnet.html 之外使用 Postal,因为正在发送的电子邮件来自后台进程。这是使用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。

有什么想法吗?

编辑: 似乎其他人也有完全相同的问题:

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?

【问题讨论】:

    标签: visual-studio razor hangfire postal


    【解决方案1】:

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

    我实现此功能的方法是将“额外”文件部署为发布的一部分,而不是让它们成为 Web 项目本身的一部分。

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

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

    在您的 *.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”的文件夹中,它们将在发布期间被复制。

    【讨论】:

      【解决方案2】:

      如果您不使用 .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> 
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 如果您查看所选答案,您将能够了解如何在发布过程中自动执行此操作。
        【解决方案4】:

        就我而言(我没有直接使用 RazorViewEngine,而是从文件系统加载 .cshtml)。我将.cshtml 更改为.html,它不再是编译的一部分。

        【讨论】:

          猜你喜欢
          • 2016-11-25
          • 1970-01-01
          • 2014-03-27
          • 2018-02-06
          • 2023-04-10
          • 1970-01-01
          • 2017-01-08
          • 1970-01-01
          • 2012-10-07
          相关资源
          最近更新 更多