【问题标题】:Is it possible to reuse Keys in Web.Config?是否可以在 Web.Config 中重用密钥?
【发布时间】:2011-10-26 04:57:11
【问题描述】:
我想在两个 Web 应用程序之间共享一个文件夹,所以我尝试执行以下操作:
<add key="SharedFolder" value="D:\tfs\PlacasV1\Aplicacion Placas DataCenter\Integracion.Reclamos\Web-PRbranch1\"/>
<add key="Claims.ControlGen.OutputDir" value="SharedFolder\restricted\controls\generated\"/>
<add key="Claims.ControlGen.CsTemplatePath" value="SharedFolder\restricted\templates\CustomFieldsControl.ascx.cs.temp"/>
<add key="Claims.ControlGen.AscxTemplatePath" value="SharedFolder\restricted\templates\CustomFieldsControl.ascx.temp.xhtml"/>
<add key="Claims.CodeGeneration.ExpressionValidatorTemplatePath" value="SharedFolder\restricted\templates\ClaimsExpressionValidator.cs.temp"/>
<add key="Claims.CodeGeneration.SrcOutputPath" value="SharedFolder\App_Code\"/>
<add key="Claims.CodeGeneration.DatatypeTemplatePath" value="SharedFolder\restricted\templates\CaseExtensionData.cs.temp"/>
<add key="Claims.CodeGeneration.LibDir" value="SharedFolder\bin"/>
<add key="Claims.Xsl.Dir" value="SharedFolder\restricted\xsl\"/>
有什么想法吗?
谢谢!
【问题讨论】:
标签:
asp.net
configuration-files
【解决方案1】:
您可以创建自定义配置部分,并将其设计为以某种方式完成您正在寻找的工作。
有关如何创建自定义配置部分的详细信息,请参阅本文:
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
这是我在我们的一个应用程序中创建的自定义配置部分的示例。只需设计该部分以满足您的需求,它应该像魅力一样工作:
public class ImportConfiguration : ConfigurationSection
{
[ConfigurationProperty("importMap")]
public ImportMapElementCollection ImportMap
{
get
{
return this["importMap"] as ImportMapElementCollection;
}
}
}
public class ImportColumnMapElement : ConfigurationElement
{
[ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
public string LocalName
{
get
{
return this["localName"] as string;
}
set
{
this["localName"] = value;
}
}
[ConfigurationProperty("sourceName", IsRequired = true)]
public string SourceName
{
get
{
return this["sourceName"] as string;
}
set
{
this["sourceName"] = value;
}
}
}
public class ImportMapElementCollection : ConfigurationElementCollection
{
public ImportColumnMapElement this[object key]
{
get
{
return base.BaseGet(key) as ImportColumnMapElement;
}
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "columnMap";
}
}
protected override bool IsElementName(string elementName)
{
bool isName = false;
if (!String.IsNullOrEmpty(elementName))
isName = elementName.Equals("columnMap");
return isName;
}
protected override ConfigurationElement CreateNewElement()
{
return new ImportColumnMapElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ImportColumnMapElement)element).LocalName;
}
}
【解决方案2】:
您不能开箱即用。
我建议你看看DslConfig。
使用DslConfig,您可以进行如下配置:
sharedFolder = "D:\tfs\PlacasV1\Aplicacion Placas DataCenter\Integracion.Reclamos\Web-PRbranch1\"
Var["SharedFolder"] = sharedFolder
Var["Claims.ControlGen.OutputDir"] = sharedFolder + "restricted\controls\generated\"
您可以通过以下方式访问配置:
var config = new DslConfig.BooDslConfiguration();
config.GetVariable<string>("SharedFolder");
config.GetVariable<string>("Claims.ControlGen.OutputDir");
【解决方案3】:
您可以从两个应用程序都可以访问的 xml 文件中读取值