visual-studio 从在 Visual Studio 中发布的网站中排除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/650875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:17:10  来源:igfitidea点击:

Exclude files from web site publish in Visual Studio

visual-studiovisual-studio-2005web-publishing

提问by Jim Snyder

Can I exclude a folder or files when I publish a web site in Visual Studio 2005? I have various resources that I want to keep at hand in the Solution Explorer, such as alternate config files for various environments, but I don't really want to publish them to the server. Is there some way to exclude them? When using other project types, such as a .dll assembly, I can set a file's Build Action property to "None" and its Copy to Output Directory property to "Do not copy". I cannot find any similar settings for files in a web site.

在 Visual Studio 2005 中发布网站时是否可以排除文件夹或文件?我想在解决方案资源管理器中保留各种资源,例如各种环境的备用配置文件,但我真的不想将它们发布到服务器。有什么办法可以排除它们吗?使用其他项目类型(例如 .dll 程序集)时,我可以将文件的构建操作属性设置为“无”,并将其复制到输出目录属性设置为“不复制”。我在网站中找不到任何类似的文件设置。

If the IDE does not offer this feature, does anyone have good technique for handling such files?

如果 IDE 不提供此功能,是否有人有处理此类文件的好技术?

采纳答案by Chris Hynes

If you can identify the files based on extension, you can configure this using the buildproviders tag in the web.config. Add the extension and map it to the ForceCopyBuildProvider. For example, to configure .xml files to be copied with a publish action, you would do the following:

如果您可以根据扩展名识别文件,则可以使用web.config 中buildproviders 标记进行配置。添加扩展并将其映射到 ForceCopyBuildProvider。例如,要配置要使用发布操作复制的 .xml 文件,您可以执行以下操作:

<configuration>...
    <system.web>...
        <compilation>...
            <buildProviders>
                <remove extension=".xml" />
                <add extension=".xml" type="System.Web.Compilation.ForceCopyBuildProvider" />
            </buildProviders>

To keep a given file from being copied, you'd do the same thing but use System.Web.Compilation.IgnoreFileBuildProvider as the type.

为了防止给定文件被复制,您可以做同样的事情,但使用 System.Web.Compilation.IgnoreFileBuildProvider 作为类型。

回答by Keith

Exclude files and folders by adding ExcludeFilesFromDeploymentand ExcludeFoldersFromDeploymentelements to your project file (.csproj, .vbproj, etc). You will need to edit the file in a text editor, or in Visual Studio by unloading the project and then editing it.

通过向项目文件(.csproj、.vbproj 等)添加ExcludeFilesFromDeploymentExcludeFoldersFromDeployment元素来排除文件和文件夹。您需要在文本编辑器或 Visual Studio 中通过卸载项目然后对其进行编辑来编辑该文件。

Add the tags anywhere within the appropriate PropertyGroup(Debug, Release, etc) as shown below:

在适当的PropertyGroup(调试、发布等)中的任意位置添加标签,如下所示:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
  ... 
  <ExcludeFilesFromDeployment>File1.aspx;Folder2\File2.aspx</ExcludeFilesFromDeployment> 
  <ExcludeFilesFromDeployment>**\.svn\**\*.*</ExcludeFilesFromDeployment>
  <ExcludeFoldersFromDeployment>Folder1;Folder2\Folder2a</ExcludeFoldersFromDeployment> 
</PropertyGroup>

Wildcards are supported.

支持通配符。

To explain the example above:

解释上面的例子:

  • The 1st ExcludeFilesFromDeploymentexcludes File1.aspx(in root of project) and Folder2\File2.aspx(Folder2is in the root of the project)
  • The 2nd ExcludeFilesFromDeploymentexcludes all files within any folder named .svnand any of its subfolders
  • The ExcludeFoldersFromDeploymentexcludes folders named Folder1(in root of project) and Folder2\Folder2a(Folder2is in the root of the project)
  • 第一个ExcludeFilesFromDeployment排除File1.aspx(在项目的根目录中)和Folder2\File2.aspxFolder2在项目的根目录中)
  • 第二个ExcludeFilesFromDeployment排除命名的任何文件夹.svn及其任何子文件夹中的所有文件
  • ExcludeFoldersFromDeployment名为排除的文件夹Folder1(在项目的根目录)和Folder2\Folder2aFolder2在项目的根目录)

For more info see MSDN blog post Web Deployment: Excluding Files and Folders via the Web Application's Project File

有关详细信息,请参阅 MSDN 博客文章Web 部署:通过 Web 应用程序的项目文件排除文件和文件夹

回答by Brian Ogden

Amazingly the answer for Visual Studio 2012 is not here:

令人惊讶的是,Visual Studio 2012 的答案并不在这里:

  • The answer with green checkmark is not the answer.

  • The highest "upped" answer references an article from 2010 and says you have to edit your csproj project file which is now incorrect. I added the ExcludeFoldersFromDeployment XML element to my Visual Studio 2012 csproj file and it did nothing, the element was considered invalid, this is because ExcludeFoldersFromDeployment has been moved to the .pubxml file it looks like.

  • 带有绿色复选标记的答案不是答案。

  • 最高的“升级”答案引用了 2010 年的一篇文章,并说您必须编辑现在不正确的 csproj 项目文件。我将 ExcludeFoldersFromDeployment XML 元素添加到我的 Visual Studio 2012 csproj 文件中,但它什么也没做,该元素被认为是无效的,这是因为 ExcludeFoldersFromDeployment 已移动到它看起来像的 .pubxml 文件。

For Web Applications and Websitesyou edit the .pubxml file!

对于 Web 应用程序和网站,您编辑 .pubxml 文件!

You can follow my answer or try this guide which I found later: http://www.leniel.net/2014/05/using-msdeploy-publish-profile-pubxml-to-create-an-empty-folder-structure-on-iis-and-skip-deleting-it-with-msdeployskiprules.html#sthash.MSsQD8U1.dpbs

您可以按照我的回答或尝试我后来找到的本指南:http: //www.leniel.net/2014/05/using-msdeploy-publish-profile-pubxml-to-create-an-empty-folder-structure- on-iis-and-skip-deleting-it-with-msdeployskiprules.html#sthash.MSsQD8U1.dpbs

Yes, you can do this not just for Website Projects but Websites too. I spent a long time on the internet looking for this elusive exclude ability with a Visual Studio Website (NOT Website project) and had previously concluded it was not possible but it looks like it is:

是的,您不仅可以为网站项目执行此操作,还可以为网站执行此操作。我花了很长时间在互联网上寻找这种难以捉摸的排除功能与 Visual Studio 网站(不是网站项目),之前已经得出结论这是不可能的,但它看起来像:

In your [mypublishwebsitename].pubxml file, found in ~/Properties/PublishProfiles for Web Application Projects and ~/App_Data/PublishProfiles for Websites, simply add:

在您的 [mypublishwebsitename].pubxml 文件中,在 Web 应用程序项目的 ~/Properties/PublishProfiles 和网站的 ~/App_Data/PublishProfiles 中找到,只需添加:

  <ExcludeFilesFromDeployment>File1.aspx;Folder2\File2.aspx</ExcludeFilesFromDeployment> 
 <ExcludeFoldersFromDeployment>Folder1;Folder2\Folder2a</ExcludeFoldersFromDeployment>

as children to the main <PropertyGroup>element in your .pubxml file. No need to add a new element not unless you are keying a specific build type, like release or debug.

作为<PropertyGroup>.pubxml 文件中主要元素的子元素。除非您键入特定的构建类型,例如发布或调试,否则无需添加新元素。

BUT WAIT!!!

可是等等!!!

If you are removing files from your destination/target server with the following setting in your Publish configuration:

如果您在发布配置中使用以下设置从目标/目标服务器中删除文件:

enter image description here

在此处输入图片说明

Then the Web Publish process will delete on your source/target server anything excluded, like an item you have delineated in your <ExcludeFoldersFromDeployment>and <ExcludeFilesFromDeployment>!

然后,Web 发布过程将删除源/目标服务器上排除的任何内容,例如您在<ExcludeFoldersFromDeployment>和 中描述的项目<ExcludeFilesFromDeployment>

MsDeploy Skip Rules to the rescue:

MsDeploy跳过规则来救援:

First, Web Publish uses something other than MSBuild to publish (called Task IO or something like that) but it has a bug and will not recognize skip rules, so you must add to your .pubxml:

首先,Web Publish 使用 MSBuild 以外的其他东西来发布(称为 Task IO 或类似的东西),但它有一个错误并且无法识别跳过规则,因此您必须添加到您的 .pubxml 中:

<PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
</PropertyGroup>

I would keep <WebPublishMethod>in its own <PropertyGroup>, you would think you could just have one <PropertyGroup>element in your .pubxml but my Skip Rules were not being called until I moved <WebPublishMethod>to its own <PropertyGroup>element. Yes, crazy, but the fact you need to do all this for Web Publish to exclude and also not delete a folder/file on your server is crazy.

我会保留<WebPublishMethod>它自己的<PropertyGroup>,你会认为你可以<PropertyGroup>在你的 .pubxml 中只有一个元素,但我的跳过规则直到我移动<WebPublishMethod>到它自己的<PropertyGroup>元素才被调用。是的,很疯狂,但是您需要为 Web Publish 执行所有这些操作才能排除并且不删除服务器上的文件夹/文件这一事实是很疯狂的。

Now my actual SkipRules, ExcludeFolders and ExcludeFiles declarations in my .pubxml:

现在我的 .pubxml 中实际的 SkipRules、ExcludeFolders 和 ExcludeFiles 声明:

<ExcludeFoldersFromDeployment>Config</ExcludeFoldersFromDeployment>
<ExcludeFoldersFromDeployment>Photos</ExcludeFoldersFromDeployment>
<ExcludeFoldersFromDeployment>Temp</ExcludeFoldersFromDeployment>
<ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>
 <AfterAddIisSettingAndFileContentsToSourceManifest>AddCustomSkipRules</AfterAddIisSettingAndFileContentsToSourceManifest>

And now a the Skip Rules (<Target>is a child of <Project>in your .pubxml): (You may be able to leave <SkipAction>empty to Skip for all actions but I didn't test that and am not sure.

现在跳过规则(<Target><Project>您的 .pubxml 中的子项):(您可以将<SkipAction>所有操作的跳过都留空,但我没有测试过,也不确定。

  <Target Name="AddCustomSkipRules">
    <Message Text="Adding Custom Skip Rules" />
    <ItemGroup>
      <MsDeploySkipRules Include="SkipConfigFolder">
        <SkipAction>Delete</SkipAction>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\Config</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
      <MsDeploySkipRules Include="SkipPhotosFolder">
        <SkipAction>Delete</SkipAction>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\Photos</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
      <MsDeploySkipRules Include="SkipWebConfig">
        <SkipAction>Delete</SkipAction>
        <ObjectName>filePath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\Web\.config</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
      <MsDeploySkipRules Include="SkipWebConfig">
        <SkipAction>Delete</SkipAction>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_DestinationContentPath)\Temp</AbsolutePath>
        <XPath>
        </XPath>
      </MsDeploySkipRules>
    </ItemGroup>
  </Target>

And please, do not to forget to escape the .in a filePath Skip rule with a backslash.

并且请不要忘记.使用反斜杠转义 filePath Skip 规则。

回答by BlackHymanetMack

I struggled with the same issue and finally pulled the trigger on converting the web site to a web application. Once I did this, I got all of the IDE benefits such as build action, and it compiled faster to boot (no more validating web site...).

我在同样的问题上苦苦挣扎,最后扣动了将网站转换为 Web 应用程序的触发器。一旦我这样做了,我就获得了所有 IDE 的好处,例如构建操作,并且它编译启动速度更快(不再验证网站......)。

Step 1: Convert your 'web site' to a 'web application'. To convert it I just created a new "web application", blew away all the files it created automatically, and copied and pasted my web site in. This worked fine. Note that report files will need to have their Build Action set to "Content" instead of "none".

第 1 步:将您的“网站”转换为“网络应用程序”。为了转换它,我刚刚创建了一个新的“网络应用程序”,清除了它自动创建的所有文件,然后复制并粘贴了我的网站。这很好用。请注意,报告文件需要将其构建操作设置为“内容”而不是“无”。

Step 2: Now you can set any files "Build Action" property.

第 2 步:现在您可以设置任何文件的“构建操作”属性。

Hope this helps.

希望这可以帮助。

回答by Simon Tewsi

In Visual Studio 2013 I found Keith's answer, adding the ExcludeFoldersFromDeploymentelement to the project file, didn't work (I hadn't read Brian Ogden's answer which says this). However, I found I could exclude a text file when publishing in Visual Studio 2013 by just setting the following properties on the text file itself:

在 Visual Studio 2013 中,我发现 Keith 的答案,将ExcludeFoldersFromDeployment元素添加到项目文件中,没有用(我没有读过 Brian Ogden 的回答,其中说明了这一点)。但是,我发现在 Visual Studio 2013 中发布时,我可以通过在文本文件本身上设置以下属性来排除文本文件:

1) Build Action: None

1) 构建动作:无

2) Copy to Output Directory: Do not copy

2)复制到输出目录:不要复制

Initially I tried setting the Copy to Output Directory property by itself but that didn't work when the Build Action was set to the default value, Content. When I then set the Build Action to None the text file was no longer copied to the destination folder when I published.

最初我尝试单独设置 Copy to Output Directory 属性,但是当 Build Action 设置为默认值 Content 时,这不起作用。当我将 Build Action 设置为 None 时,文本文件在我发布时不再复制到目标文件夹。

To view these properties in the Visual Studio GUI, in the Solution Explorer right-click on the file you want to exclude and select Properties from the context menu.

要在 Visual Studio GUI 中查看这些属性,请在解决方案资源管理器中右键单击要排除的文件,然后从上下文菜单中选择属性。

回答by Kev

I think you only have two options here:

我认为你在这里只有两个选择:

  • Use the 'Exclude From Project' feature. This isn't ideal because the project item will be excluded from any integrated IDE source control operations. You would need to click the 'Show All Files' button on the Solution window if you need to see the files in Solution Explorer, but that also shows files and folders you're not interested in.
  • Use a post-build event script to remove any project items you don't want to be published (assuming you're publishing to a local folder then uploading to the server).
  • 使用“从项目中排除”功能。这并不理想,因为项目项将从任何集成的 IDE 源代码控制操作中排除。如果您需要在解决方案资源管理器中查看文件,则需要单击“解决方案”窗口上的“显示所有文件”按钮,但这也会显示您不感兴趣的文件和文件夹。
  • 使用构建后事件脚本删除您不想发布的任何项目项(假设您要发布到本地文件夹然后上传到服务器)。

I've been through this before and couldn't come up with anything really elegant.

我以前经历过这种情况,但想不出任何真正优雅的东西。

回答by Nico Dreyer

For Visual Studio 2017, WebApp Publish, first create a standard file system publish profile. Go to the App_Data\PublishProfiles\ folder and edit the [profilename].pubxml file.

对于 Visual Studio 2017 WebApp Publish,首先创建标准文件系统发布配置文件。转到 App_Data\PublishProfiles\ 文件夹并编辑 [profilename].pubxml 文件。

Add

添加

<ExcludeFilesFromDeployment>[file1.ext];[file2.ext];[file(n).ext]</ExcludeFilesFromDeployment>

under the tag<PropertyGroup>You can only specify this tag once, otherwise it will only take the last one's values.

在标签下<PropertyGroup>只能指定一次这个标签,否则只会取最后一个的值。

Example:

例子:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>C:\inetput\mysite</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
    <ExcludeFilesFromDeployment>web.config;mysite.sln;App_Code\DevClass.cs;</ExcludeFilesFromDeployment>
  </PropertyGroup>
</Project>

Make sure that the tag DeleteExistingFiles is set to False

确保标记 DeleteExistingFiles 设置为False

回答by David

The feature you are looking exists if your project is created as a "Web Application". Web Site"projects" are just a collection of files that are thought of as 1:1 with what gets deployed to a web server.

如果您的项目是作为“Web 应用程序”创建的,那么您正在寻找的功能就存在。网络站点“项目”仅仅是被认为是1个文件的集合:什么被部署到Web服务器1。

In terms of functionality both are the same, however a web application compiles all source code to a DLL, instead of the naked source code files being copied to the web server and compiled as needed.

就功能而言,两者是相同的,但是 Web 应用程序将所有源代码编译为 DLL,而不是将裸源代码文件复制到 Web 服务器并根据需要进行编译。

回答by Markus R.

In Visual Studio 2017 (15.9.3 in my case) the manipulation of the .csproj-File works fine indeed! No need to modify the pubxml.

在 Visual Studio 2017(在我的情况下为 15.9.3)中,.csproj-File 的操作确实可以正常工作!无需修改pubxml。

You can then construct pretty nice settings in the .csproj-File using the PropertyGroup condition, e.g.:

然后,您可以使用 PropertyGroup 条件在 .csproj-File 中构建非常好的设置,例如:

  <PropertyGroup Condition="$(Configuration.StartsWith('Pub_'))">
    <ExcludeFoldersFromDeployment>Samples</ExcludeFoldersFromDeployment>
  </PropertyGroup>

excludes the "Samples" folder from all deployments with configurations starting with "Pub_"...

从配置以“Pub_”开头的所有部署中排除“Samples”文件夹...

回答by Alex KeySmith

As a contemporary answer, in Visual Studio 2017 with a .net core site:

作为当代答案,在带有 .net 核心站点的 Visual Studio 2017 中:

You can exclude from publish like so in the csproj, where CopyToPublishDirectory is never.

您可以像在 csproj 中那样从发布中排除,其中 CopyToPublishDirectory 从不。

  <ItemGroup>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
    <Content Update="appsettings.Local.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

This is discussed in more detail here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2

这里有更详细的讨论:https: //docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2

<PropertyGroup>
    <ExcludeFilesFromDeployment>appsettings.Local.json</ExcludeFilesFromDeployment>
</PropertyGroup>

The earlier suggestions did not work for me, I'm guessing because visual studio is now using a different publishing mechanism underneath, I presume via the "dotnet publish" cli tool or equivalent underneath.

早期的建议对我不起作用,我猜是因为 Visual Studio 现在在下面使用不同的发布机制,我推测是通过“dotnet publish”cli 工具或下面的等效工具。