有人知道有没有一种方法可以"转换"值的特定部分,而不是替换整个值或属性?

例如,我有几个appSettings条目,用于指定不同Web服务的URL.这些条目在开发环境中与生产环境中略有不同.有些人不那么琐碎

<!-- DEV ENTRY -->
<appSettings>
 <add key="serviceName1_WebsService_Url" value="http://wsServiceName1.dev.domain.com/v1.2.3.4/entryPoint.asmx" />
 <add key="serviceName2_WebsService_Url" value="http://ma1-lab.lab1.domain.com/v1.2.3.4/entryPoint.asmx" />
</appSettings>

<!-- PROD ENTRY -->
<appSettings>
 <add key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.domain.com/v1.2.3.4/entryPoint.asmx" />
 <add key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.domain.com/v1.2.3.4/entryPoint.asmx" />
</appSettings>

请注意,在第一个条目上,第二个条目的唯一差异是".dev" from ".prod".,子域是不同的:"ma1-lab.lab1""ws.ServiceName2"

到目前为止,我知道我可以在网上做类似的事情.释放配置:

<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.domain.com/v1.2.3.4/entryPoint.asmx" />
<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.domain.com/v1.2.3.4/entryPoint.asmx" />

然而,每次更新Web服务的版本时,我都必须更新Web.释放配置,这违背了简化我的网络的目的.配置更新.

我知道我也可以将该URL分成不同的部分,然后单独更新它们,但我更愿意将其集中在一个键中.

我已经查看了可用的web.config转换,但似乎没有任何东西与我想要实现的目标相匹配.

以下是我用作参考的网站:

Vishal Joshi's blogMSDN HelpChannel9 video

任何帮助都将不胜感激!

-D

推荐答案

事实上,你可以做到这一点,但这并不像你想象的那么容易.您可以创建自己的配置转换.我刚刚写了一篇关于这一点的非常详细的博客文章.但这里有重点:

  • 创建类库项目
  • 参考Web.Publishing.Tasks.dll (under %Program Files (x86)%MSBuild\Microsoft\VisualStudio\v10.0\Web folder)
  • 扩展微软.网状物出版业.任务.变换类
  • 实现Apply()方法
  • 将部件放置在熟知的位置
  • 使用xdt:import使新转换可用
  • 使用变换

Here is the class I created to do this replace

namespace CustomTransformType
{
    using System;
    using System.Text.RegularExpressions;
    using System.Xml;
    using Microsoft.Web.Publishing.Tasks;

    public class AttributeRegexReplace : Transform
    {
        private string pattern;
        private string replacement;
        private string attributeName;

        protected string AttributeName
        {
            get
            {
                if (this.attributeName == null)
                {
                    this.attributeName = this.GetArgumentValue("Attribute");
                }
                return this.attributeName;
            }
        }
        protected string Pattern
        {
            get
            {
                if (this.pattern == null)
                {
                    this.pattern = this.GetArgumentValue("Pattern");
                }

                return pattern;
            }
        }

        protected string Replacement
        {
            get
            {
                if (this.replacement == null)
                {
                    this.replacement = this.GetArgumentValue("Replacement");
                }

                return replacement;
            }
        }

        protected string GetArgumentValue(string name)
        {
            // this extracts a value from the arguments provided
            if (string.IsNullOrWhiteSpace(name)) 
            { throw new ArgumentNullException("name"); }

            string result = null;
            if (this.Arguments != null && this.Arguments.Count > 0)
            {
                foreach (string arg in this.Arguments)
                {
                    if (!string.IsNullOrWhiteSpace(arg))
                    {
                        string trimmedArg = arg.Trim();
                        if (trimmedArg.ToUpperInvariant().StartsWith(name.ToUpperInvariant()))
                        {
                            int start = arg.IndexOf('\'');
                            int last = arg.LastIndexOf('\'');
                            if (start <= 0 || last <= 0 || last <= 0)
                            {
                                throw new ArgumentException("Expected two ['] characters");
                            }

                            string value = trimmedArg.Substring(start, last - start);
                            if (value != null)
                            {
                                // remove any leading or trailing '
                                value = value.Trim().TrimStart('\'').TrimStart('\'');
                            }
                            result = value;
                        }
                    }
                }
            }
            return result;
        }

        protected override void Apply()
        {
            foreach (XmlAttribute att in this.TargetNode.Attributes)
            {
                if (string.Compare(att.Name, this.AttributeName, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    // get current value, perform the Regex
                    att.Value = Regex.Replace(att.Value, this.Pattern, this.Replacement);
                }
            }
        }
    }
}

Here is web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="one" value="one"/>
    <add key="two" value="partial-replace-here-end"/>
    <add key="three" value="three here"/>
  </appSettings>
</configuration>

以下是我的配置转换文件

<?xml version="1.0"?>

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

  <xdt:Import path="C:\Program Files (x86)\MSBuild\Custom\CustomTransformType.dll"
              namespace="CustomTransformType" />

  <appSettings>
    <add key="one" value="one-replaced" 
         xdt:Transform="Replace" 
         xdt:Locator="Match(key)" />
    <add key="two" value="two-replaced" 
         xdt:Transform="AttributeRegexReplace(Attribute='value', Pattern='here',Replacement='REPLACED')" 
         xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

Here is the result after transformation

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="one" value="one-replaced"/>
    <add key="two" value="partial-replace-REPLACED-end"/>
    <add key="three" value="three here"/>
  </appSettings>
</configuration>

Asp.net相关问答推荐

缓存httpmessage内容

授权错误错误 400:C# 上的 redirect_uri_mismatch

如何正确配置 IHttpModule?

web.config 部分的单独配置文件

我可以在一个 Web 项目中有多个 web.config 文件吗?

asp.net 单选按钮分组

ASP.NET 如何将控件呈现为 HTML?

如何在 ASP.NET 中使用时区?

无法访问,内部,资源文件?

在属性中实现逻辑是一种好习惯吗

将 ListItem 的 ASP.NET 列表数据绑定到 DropDownList 问题

修改 web.config 时如何防止 ASP.NET 应用程序重新启动?

C# 7 本地函数未按预期工作且未显示错误

ASP.Net 无法创建/卷影复制

global.asax 中的 Application_Error 未捕获 WebAPI 中的错误

使用 ConfigurationManager.RefreshSection 重新加载配置而不重新启动应用程序

从物理路径获取相对虚拟路径

ASP.NET 核心,更改未经授权的默认重定向

SignalR 2.0 错误:无法加载文件或程序集 Microsoft.Owin.Security

ModelClientValidationRule 冲突