Exclude files from web site publish in Visual Studio

后端 未结 11 1888
醉话见心
醉话见心 2020-11-27 12:44

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 alte

相关标签:
11条回答
  • 2020-11-27 12:55

    This is just an addendum to the other helpful answers here and something I've found useful...

    Using wpp.targets to excluded files and folders

    When you have multiple deployments for different environments then it's helpful to have just one common file where you can set all the excluded files and folders. You can do this by creating a *.wpp.targets file in the root of the project like the example below.

    For more information see this Microsoft guide:

    How to: Edit Deployment Settings in Publish Profile (.pubxml) Files and the .wpp.targets File in Visual Studio Web Projects

    <?xml version="1.0" encoding="utf-8" ?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <EnableMSDeployAppOffline>True</EnableMSDeployAppOffline>
        <ExcludeFilesFromDeployment>
          *.config;
          *.targets;
          *.default;
        </ExcludeFilesFromDeployment>
        <ExcludeFoldersFromDeployment>
          images;
          videos;
          uploads;
        </ExcludeFoldersFromDeployment>
      </PropertyGroup>
    </Project>
    
    0 讨论(0)
  • 2020-11-27 13:00

    In Visual Studio 2013 I found Keith's answer, adding the ExcludeFoldersFromDeployment element 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:

    1) Build Action: None

    2) Copy to Output Directory: Do not copy

    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.

    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.

    0 讨论(0)
  • 2020-11-27 13:04

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

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

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

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 13:06

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

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

    0 讨论(0)
  • 2020-11-27 13:06

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

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

      <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

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

    0 讨论(0)
  • 2020-11-27 13:08

    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.

    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.

    0 讨论(0)
提交回复
热议问题