Die*_* C. 41 asp.net web-config transformation visual-studio-2010 web-deployment
有没有人知道是否有办法"转换"值的特定部分而不是替换整个值或属性?
例如,我有几个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>
Run Code Online (Sandbox Code Playgroud)
请注意,在第一个条目中,唯一的区别是".prod"中的".dev".在第二项,子域名是不同的:"MA1-lab.lab1"从"ws.ServiceName2"
到目前为止,我知道我可以在Web中做这样的事情.Release.Config:
<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" />
Run Code Online (Sandbox Code Playgroud)
但是,每次更新该Web服务的版本时,我都必须更新Web.Release.Config,这会破坏我的web.config更新的目的.
我知道我也可以将该URL拆分为不同的部分并单独更新它们,但我宁愿将它全部放在一个键中.
我已经浏览了可用的web.config Transforms但是nothings似乎是我想要完成的东西.
这些是我用作参考的网站:
Vishal Joshi的博客,MSDN帮助和Channel9视频
任何帮助将非常感激!
-D
Say*_*imi 67
事实上你可以做到这一点,但它并不像你想象的那么容易.您可以创建自己的配置转换.我刚刚在http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx上写了一篇非常详细的博客文章.但这里有高光:
这是我为替换而创建的类
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);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
这是我的配置转换文件
<?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>
Run Code Online (Sandbox Code Playgroud)
这是转换后的结果
<?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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11292 次 |
| 最近记录: |