Exclude files from web site publish in Visual Studio

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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.

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

回答1:

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:

...     ...         ...             

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



回答2:

You can exclude folders and files by adding ExcludeFilesFromDeployment and ExcludeFoldersFromDeployment elements to your project file (.csproj, .vbproj, etc). Add them within the appropriate PropertyGroup as shown below:

    ...    File1.aspx;Folder2\File2.aspx**\.svn\**\*.*Folder1;Folder2\Folder2a

Wildcards are supported.

In 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 Web Deployment: Excluding Files and Folders via the Web Application’s Project File



回答3:

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:

  File1.aspx;Folder2\File2.aspxFolder1;Folder2\Folder2a

as children to the main 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:

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

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:

MSDeploy

I would keep in its own , you would think you could just have one element in your .pubxml but my Skip Rules were not being called until I moved to its own 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:

ConfigPhotosTempWeb.configAddCustomSkipRules

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

  DeletedirPath$(_DestinationContentPath)\\ConfigDeletedirPath$(_DestinationContentPath)\\PhotosDeletefilePath$(_DestinationContentPath)\\Web\.configDeletedirPath$(_DestinationContentPath)\\Temp

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



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!