如何使用 Visual Studio 网站项目进行 Web.config 转换?
似乎无法更改 Visual Studio 2010 网站的构建配置项目(与 Visual Studio Web 应用程序相反),更改构建配置是启用 Web.config 转换的关键部分(不可能将配置更改为除调试之外的任何配置)。
如果无法更改生成配置,如何使 Web.config 转换与 Visual Studio 2010 网站项目一起使用?
It doesn't seem to be possible to change the Build Configuration of Visual Studio 2010 Website Projects (as opposed to Visual Studio Web Applications), and changing the Build Configuration is a key part of enabling Web.config transformations (it's not possible to change the configuration to anything except Debug).
How do I get Web.config transformations to work with Visual Studio 2010 Website projects if it's not possible to change the Build Configuration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现了一篇非常好的博客文章,描述了这个问题的解决方案:
http://andrewtwest.com/ 2010/02/25/using-web-config-transformations-in-web-site-projects/
简而言之:在您的解决方案中创建一个空项目(只要它不是另一个网站项目),其中包含网站。空项目将允许您通过其项目文件访问 msbuild,这将允许您在网站 web.config 上执行转换。
I found a pretty good blog post describing a solution to this here:
http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/
In short: create an empty project (as long as it is not another website project) in your solution that contains the website. The empty project will give you access to msbuild through its project file, which will allow you to perform transforms on your website web.config.
我使用了一种稍微另类的方法。虽然还是有点 hack,但我认为更简单。这对我有用,但显然有很多不同的配置可用,所以我不能保证它适合每个人。这围绕着网站在发布之前首先打包到您的
AppData
文件夹中的方式...手动添加
Web.Release.config
文件到网站并添加必要的转换 - 显然网站没有“添加配置转换”选项,因此必须手动执行此操作。 Web.Release.config 示例:在
website.publishproj
文件中,确保配置设置为 Release:将以下内容添加到
website.publishproj
的最底部>(就在之前):
I used a slightly alternative approach. Still a bit of a hack, but I think a lot more straightforward. This worked for me, but obviously there are a lot of different configurations available so I can't guarantee it'll work for everyone. This revolves around the way that a website is first packaged up in to your
AppData
folder before being published...Manually add a
Web.Release.config
file to the website and add the necessary transforms - obviously there's no 'Add Config Transform' option for websites, hence having to do this manually. Example Web.Release.config:Inside the
website.publishproj
file, ensure the configuration is set to Release:Add the following to the very bottom of
website.publishproj
(just before</Project>
):在 VS 2017 中创建发布配置文件,然后右键单击 App_Data\PublishProfiles 中的 .pubxml 配置文件,然后选择“添加配置转换”。
请参阅https://social.msdn.microsoft.com/Forums/vstudio/en-US/31f41991-abb2-41de-ad0b-c1379cc7c806/vs-2013-express -for-web-webconfig-transforms?forum=visualstudiogeneral&prof=required
Create a publish profile in VS 2017 and then right click on the .pubxml profile in App_Data\PublishProfiles and select Add Config Transform.
See https://social.msdn.microsoft.com/Forums/vstudio/en-US/31f41991-abb2-41de-ad0b-c1379cc7c806/vs-2013-express-for-web-webconfig-transforms?forum=visualstudiogeneral&prof=required
正如上面 Andriy 的评论中提到的,解决方案范围的构建事件绝对看起来是一种更干净的方式来做到这一点。
我将其添加为单独的答案,因为它在评论中有点丢失,但恕我直言,这是最好的答案。支持 Andriy K 和 Sayed Ibrahim。
As mentioned in Andriy's comment above, Solution wide build events definitely seems like a cleaner way to do this.
I am adding this as a separate answer, as it gets a little lost in the comment, but IMHO is the best answer. Props to Andriy K and Sayed Ibrahim.
如果您不想需要 Web.Template.config,我使用了这个:
改编自答案
If you would prefer not to need a Web.Template.config, I used this:
Adapted from an answer here.
我不想直接使用整个 Web 应用程序项目解决方案。
我的解决方案是直接使用Microsoft.Web.Publishing.Tasks.dll中定义的XmlTransform任务(这个任务是WebConfigTransformation的核心)
这样它就足够灵活并且完全按照您的期望进行操作。
例如,这里是我用于转换 web.config 的 WebSiteTransformator.csproj。
这里还有一个原始 WebConfigTransformation 无法实现的灵活性示例:它采用 web.Template.config,在其上应用 web.$(Configuration).config 并写入 web.config。这允许我们将 web.config 本身添加到源代码管理中的忽略列表中。网站引用的 csproj 仍然有效:
I'd prefer not to use entire an Web Application Project solution out of box.
My solution is to use the XmlTransform task defined in Microsoft.Web.Publishing.Tasks.dll directly (this task is the core of WebConfigTransformation)
This way it is flexible enough and does exactly what you expect it to do.
For example here is the WebSiteTransformator.csproj I'm using for transforming web.config.
Here also is an example of flexibility that is impossible to reach with original WebConfigTransformation: it takes web.Template.config, applies web.$(Configuration).config over it and writes web.config. This allows us to add web.config itself into ignore list in source control. It is still valid csproj to be referenced by website: