Exclude files from web site publish in Visual Studio

后端 未结 11 1889
醉话见心
醉话见心 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 13:09

    Exclude files and folders by adding ExcludeFilesFromDeployment and ExcludeFoldersFromDeployment elements 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.

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

    <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 ExcludeFilesFromDeployment excludes File1.aspx (in root of project) and Folder2\File2.aspx (Folder2 is in the root of the project)
    • The 2nd ExcludeFilesFromDeployment excludes all files within any folder named .svn and any of its subfolders
    • The ExcludeFoldersFromDeployment excludes folders named Folder1 (in root of project) and Folder2\Folder2a (Folder2 is in the root of the project)

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

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

    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:

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

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

    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.

    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.

    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

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

    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.

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

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

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

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

    Amazingly the answer for Visual Studio 2012 is not here:

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

    For Web Applications and Websites you edit the .pubxml file!

    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

    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:

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

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

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

    MsDeploy Skip Rules to the rescue:

    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:

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

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

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

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