Web.config Build vs Release 转换不起作用 [英] Web.config Build vs Release transform not working

查看:27
本文介绍了Web.config Build vs Release 转换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET Web 应用程序项目,它通过实体框架连接到远程数据库.在调试期间(例如在我的本地计算机上运行项目),数据库的 IP 地址与发布期间(例如在将项目上传到我的网络服务器并从浏览器运行之后)不同.到目前为止,我一直手动更改 Web.config 文件中的数据库连接字符串以在两者之间切换(基本上我必须连接字符串,一个名为Debug",一个名为Release",每当我部署时我只是交换名称).

I have an ASP.NET Web Application project that connects to a remote database via the Entity Framework. During debugging (eg running the project on my local computer), the IP address to the database is different than during release (eg after uploading the project to my webserver and running it from the browser). Until now I have always manually changed the database connection string in the Web.config file to switch between the two (basically I had to connection strings, one named 'Debug' and one 'Release' and I just swapped around the names whenever I deployed).

现在我注意到应该可以通过 Web.config 转换语法,您将修改后的连接字符串放在 Web.Release.config 版本中,然后在发布配置下构建 DLL 时应该使用它.

Now I just noticed that it should be possible to let this happen automatically via the Web.config Transformation Syntax where you put the modified connection string in the Web.Release.config version and it should then use that when the DLL is built under Release configuration.

然而它似乎对我不起作用......

However it does not seem to work for me...

这是我的常规 Web.config 文件的相关部分(保存本地使用的调试连接字符串):

Here is the relevant part of my regular Web.config file (which holds the Debug connection string for local usage):

<?xml version="1.0"?>
<configuration>

  <connectionStrings>
    <!-- Debug connection string. Release connection string is in Web.Release.config file -->
    <add name="DatabaseEntities" connectionString="A" providerName="System.Data.EntityClient" />
  </connectionStrings>

</configuration>

这是 Web.Release.config 文件,根据示例,如果 DLL 处于发布模式,则应将DatabaseEntities"连接字符串A"替换为B":

Here is the Web.Release.config file, which according to the examples should replace the 'DatabaseEntities' connection string "A" with "B" if the DLL is under Release mode:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <!-- Replace the DatabaseEntities connection string with the Release version (local IP address) -->
  <connectionStrings>
    <add name="DatabaseEntities"
      connectionString="B"
      xdt:Transform="Replace" xdt:Locator="Match(name)"/>
  </connectionStrings>

</configuration>

(显然A"和B"只是我真实连接字符串的占位符)

(Obviously "A" and "B" are just place-holders for my real connection strings)

当我调试应用程序时(例如,只需按 F5)使用默认的 Web.config,我可以访问数据库.然后我通过配置管理器将构建配置更改为发布.解决方案中的所有项目都设置为发布配置.然后我构建解决方案(仅通过构建或什至通过完全重建(例如清理、重建)).我将新建的 DLL 以及 Web.config 和 Web.Release.config 文件上传到 web 服务器,当我尝试访问数据库时,我无法访问,它仍在尝试通过调试 IP 地址访问数据库因此找不到它...

When I debug the application (e.g. just press F5) the default Web.config is used and I can access the database. I then change the build configuration to Release via the Configuration Manager. All the projects in the solution are set to Release configuration. Then I Build the solution (just via Build or even via a complete rebuild (e.g. Clean, Rebuild)). I upload the newly built DLLs to the webserver, as well as the Web.config and Web.Release.config files, and when I try to access the database I am unable, it is still trying to access the database via the debug IP address and hence cannot find it...

似乎 Web.Release.config 文件被完全忽略了,或者至少连接字符串没有被替换.

It seems the Web.Release.config file is completely ignored, or at least the connection string is not being replaced.

我做错了什么?转换语法错误吗?我在发布模式下没有正确构建应用程序吗?

What am I doing wrong? Is the transformation syntax wrong? Am I not building the application under Release mode correctly?

推荐答案

然后我构建解决方案(仅通过构建甚至通过完整的重建(例如清理、重建)).我将新建的 DLL 上传到网络服务器,以及 Web.config 和 Web.Release.config 文件

Then I Build the solution (just via Build or even via a complete rebuild (e.g. Clean, Rebuild)). I upload the newly built DLLs to the webserver, as well as the Web.config and Web.Release.config files

这是您的错误:如果您只是构建,Web 配置转换将不适用于您的本地环境.您需要发布.

There is your error: Web config transforms won't work for your local environment, if you simply build. You need to publish.

您的部署过程似乎很奇怪:您只是在复制 DLL、Web.config 和 web.Release.config.在我看来,您复制的是源代码而不是编译后的应用程序.已发布的 Web 应用程序不包含 web.release.config.

Your deployment process seems weird: You are only copying DLLs, Web.config and web.Release.config. To me it seems, that you copy your source code and not a compiled application. A published WebApplication doesn't contain a web.release.config.

您应该将您的项目(右键单击您的 Web 应用程序 -> 发布)发布到您的本地文件系统并从那里复制文件,或者使用您选择的其他部署方法.

You should publish your project (rightclick on your WebApplication -> Publish) to your local filesystem and copy the files from there, or use another deployment method of your choice.

2 年前我写了一篇关于 web.config 转换的文章.它为您提供了 VS 2010 的分步教程(VS 2012 中的发布对话框已更改):http://www.tomot.de/en-us/article/5/asp.net/how-to-use-web.config-transforms-to-replace-appsettings-and-connectionstrings

2 years ago I wrote an article about web.config transforms. It gives you a step-by-step tutorial for VS 2010 (The publish dialog changed in VS 2012): http://www.tomot.de/en-us/article/5/asp.net/how-to-use-web.config-transforms-to-replace-appsettings-and-connectionstrings

这篇关于Web.config Build vs Release 转换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆