Is it possible to reuse Keys in Web.Config?

房东的猫 提交于 2020-01-04 12:48:55

问题


I want to share a folder between two web applications, so I tried to do the following:

<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\"/>

Any Ideas?

Thanks!


回答1:


You can create a custom configuration section, and design it to do what you're looking for, in some way or the other.

See this article for details on how to create custom configuration sections:

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

Here is an example of a custom configuration section that I created in one of our applications. Just design the section to accomodate your needs, and it should work like a charm:

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:


You can't do this out of the box. I would suggest you to have look at DslConfig.

With DslConfig you can configure something like:

sharedFolder = "D:\tfs\PlacasV1\Aplicacion Placas DataCenter\Integracion.Reclamos\Web-PRbranch1\"
Var["SharedFolder"] = sharedFolder
Var["Claims.ControlGen.OutputDir"] = sharedFolder + "restricted\controls\generated\"

You can access the configuration with:

var config = new DslConfig.BooDslConfiguration();
config.GetVariable<string>("SharedFolder");
config.GetVariable<string>("Claims.ControlGen.OutputDir");



回答3:


You can read the values from a xml file that can be accessed by both applications



来源:https://stackoverflow.com/questions/7047181/is-it-possible-to-reuse-keys-in-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!