Web.config 转换变量

发布于 2024-10-20 15:20:07 字数 1518 浏览 5 评论 0原文

是否可以在 web.config 转换文件中包含变量?对于每个环境,我都有基本相同的变换,只是具有不同的值。例如,对于开发环境,我会...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="serverName" value="server1" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\server1" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

对于 QA 环境,我会...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="serverName" value="server2" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\server2" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

唯一的区别是 server1 与 server2 的值。这是一个简单的示例,实际上,我在转换中多次使用服务器值。有什么方法可以在转换文件中声明一个变量以供多次使用吗?像...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <property name="server" value="server2" />
    <appSettings>
        <add key="serverName" value="${server}" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\${server}" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

Is it possible to include variables in a web.config transform file? For each environment, I have basically the same transform, just with different values. For example, for the development environment, I'd have...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="serverName" value="server1" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\server1" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

And for the QA environment, I'd have...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="serverName" value="server2" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\server2" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

The only difference is the value for server1 vs server2. This is a simple example, and in reality, I use the server value multiple times in the transform. Is there any way to declare a variable in the transform file to be used multiple times? Something like...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <property name="server" value="server2" />
    <appSettings>
        <add key="serverName" value="${server}" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\${server}" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-10-27 15:20:07

web.config 转换不支持此功能。您可以看一下的一件事是创建一个 T4 模板可用于生成 web.config 转换。因此,我们的想法是将变量放置在 T4 模板中,它会输出 web.debug.config/web.release.config/etc。然后,当您打包/发布时,它只会获取作为 T4 输出生成的转换文件。

如果您能提供一些有用的具体示例,我不介意帮助您解决此类问题。

this is not supported with web.config transformations. One thing that you could take a look at is creating a T4 Template which can be used to generate your web.config transformations. So the idea being that you place the variables in the T4 template, and it will spit out the web.debug.config/web.release.config/etc. Then when you package/publish it will just pick up the transform file which was produced as the T4 output.

I wouldn't mind helping you with such a thing if you can provide some concrete examples of this being useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文