基于环境构建.Net Web应用程序 [英] Build .Net Web application based on environment

查看:45
本文介绍了基于环境构建.Net Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net Web应用程序可通过3种环境进行开发,发布和生产,每种环境都有自己的配置和项目设置文件.

There are 3 environments through which my .Net web application goes namely Development, Release and Production with each having their own config and project setting files.

假设不同环境的设置和配置文件位于一个系统中,我想创建一个小脚本或一个应用程序,其中开发人员只需提及环境类型,然后加载相关的设置和配置文件,然后构建应用程序

Assuming that the setting and config files for different environments are in one system, I want to create a small script or an application where the developer just mentions the environment type and the related setting and config files get loaded and then the application builds.

有人可以指导我吗?

推荐答案

您可以创建配置转换,并在发布配置文件中使用它们.对于每种配置(调试,发行版,YourOwnConfig ...),将有一个由其配置命名的文件(Web.Debug.config,Web.Release.Config,Web.YourOwn.Config等)

You can create config transforms and use them in publish profiles. For each configuration (Debug, Release, YourOwnConfig ...) there will be a file named by its configuration (Web.Debug.config, Web.Release.Config, Web.YourOwn.Config, ...)

诀窍是,您拥有一个完整的配置文件,即原始的Web.Config,并且转换仅通过XSLT转换语法提及了此文件的差异(一旦创建了新的转换,该文件中将包含一些示例.本身显示语法).例如,为 appSettings 键添加一个转换如下:

The trick is that you have one complete config file, the original Web.Config, and the transforms just mention the differences to this file via XSLT transform syntax (once you create a new transform, there will be some examples in the file itself showing the syntax). For example, adding a transform for an appSettings key looks like:

<configuration>
    <appSettings>
        <add key="ClientSessionTimeout" value="100"
            xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

该示例将用指定的设置替换现有的 ClientSessionTimeout 设置(使用 value ="100" ).请注意, xdt:locator 如何指定将使用key属性来本地化设置,而 xdt:Transform 如何指定所提及的属性(此处为: value ).

That example will replace an existing ClientSessionTimeout setting by the one specified (with value="100"). Notice how the xdt:locator specifies that the key attribute will be used to localize the setting, and xdt:Transform specifies that the attributes mentioned (here: value) will be set.

如果您具有 applicationSettings ,则需要替换设置本身:

If you have applicationSettings, you need to replace the setting itself:

<applicationSettings>
    <WebApplication2.Properties.Settings>
        <setting name="Setting" serializeAs="String"
                                xdt:Transform="Replace" xdt:Locator="Match(key)">
            <value>Some value</value>
        </setting>
    </WebApplication2.Properties.Settings>
</applicationSettings>

差异将是例如数据源设置,其他特定于环境的设置,例如Web服务的URL等.

The differences will be for example the data source settings, other environment specific settings such as URLs to web services etc.

要创建这些,请选择一个配置,例如"Debug",然后右键单击Web.Config文件,您将看到一个上下文菜单项"Add config transform"-单击它并进行Web.Debug.Config转换文件将在Web.Config下创建.如前所述对其进行调整;复制整个密钥或设置到转换文件,然后添加相应的 xdt 属性,如上所示.

To create those, select a configuration such as "Debug", then right-click on the Web.Config file and you will see a context menu item "Add config transform" - click it and the Web.Debug.Config transform file will be created underneath the Web.Config. Adapt it as mentioned before; copy the entire key or setting to the transform file, then add the appropriate xdt attributes as shown above.

最后,您可以使用发布"功能(在Web程序上单击鼠标右键将其选中).将打开一个向导,您可以在其中设置发布配置文件.在这里您可以提及配置-例如调试",发布"以及您之前创建的配置.

Finally, you can use the "Publish" function (Right-Click on the web prroject to select it). A wizard opens where you can set up a publish profile. There you can mention a configuration - like "Debug", "Release", and the ones you've created earlier.

文件发布将把部署Web项目所需的文件放在一起,并通过应用适当的转换文件(例如Web.Release.Config)另外执行Web.Config的转换.已发布的配置将命名为"Web.Config",其中包含所有更改.

A file publish will put the files together needed to deploy the web project and additionally perform the transformation of the Web.Config by applying the appropriate transform file (e.g. Web.Release.Config). The published config will be named "Web.Config" and contains all changes.

有关问题排查,并且要了解有关该主题的更多信息,我建议使用以下链接:

For trouble-shooting, and to find out more about the topic, I recommend the following links:

web.config转换不起作用

还要注意Stack溢出的侧栏,其中显示了更多相关链接.

Notice also the side-bar of Stack overflow showing more related links.

这篇关于基于环境构建.Net Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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