是否可以重用Web.Config中的键?

问题描述 投票:2回答:4

我想在两个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
4个回答
2
投票

您可以创建一个自定义配置部分,并将其设计为以某种方式执行您想要的工作。

有关如何创建自定义配置部分的详细信息,请参见本文:

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;
    }
}

1
投票

您不能开箱即用。我建议您看看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");

0
投票

您可以从两个应用程序都可以访问的xml文件中读取值


0
投票

我的库Expansive设计为主要用例。

中等示例(使用AppSettings作为令牌扩展的默认源)

在app.config中:

<configuration>
    <appSettings>
        <add key="Domain" value="mycompany.com"/>
        <add key="ServerName" value="db01.{Domain}"/>
    </appSettings>
    <connectionStrings>
        <add name="Default" connectionString="server={ServerName};uid=uid;pwd=pwd;Initial Catalog=master;" provider="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

在要扩展的字符串上使用。Expand()扩展方法:

var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"

如下使用动态ConfigurationManager包装器“ Config”(无需显式调用Expand()):

var serverName = Config.AppSettings.ServerName;
// returns "db01.mycompany.com"

var connectionString = Config.ConnectionStrings.Default;
// returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"

高级示例1(使用AppSettings作为令牌扩展的默认源)

在app.config中:

<configuration>
    <appSettings>
        <add key="Environment" value="dev"/>
        <add key="Domain" value="mycompany.com"/>
        <add key="UserId" value="uid"/>
        <add key="Password" value="pwd"/>
        <add key="ServerName" value="db01-{Environment}.{Domain}"/>
        <add key="ReportPath" value="\\{ServerName}\SomeFileShare"/>
    </appSettings>
    <connectionStrings>
        <add name="Default" connectionString="server={ServerName};uid={UserId};pwd={Password};Initial Catalog=master;" provider="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

在要扩展的字符串上使用.Expand()扩展方法:

var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01-dev.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
© www.soinside.com 2019 - 2024. All rights reserved.