我想在我的 web.config 中定义一个变量,我可以在我的 web.config 文件(和其他配置文件)中的多个位置使用它。通过示例可能更容易解释......
web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AuthServiceEndPoint" value="any_old_name_i_like"/>
</appSettings>
<system.web>
...
<system.serviceModel>
<client>
<endpoint
address="net.tcp://localhost/AuthService"
binding="netTcpBinding"
contract="MyServices.Contracts.IAuthService"
name="#{AppSettings.AuthServiceEndPoint}"
bindingConfiguration="netTcpBindingConfig"
/>
</client>
</system.serviceModel>
</configuration>
Windsor.config
<?xml version="1.0" encoding="utf-8" ?>
<castle>
<components>
...
<component
id="AuthProvider"
service="MyServices.Client.IAuthProvider, MyServices.Client"
type="MyServices.Client.AuthProvider, MyServices.Client"
lifestyle="transient">
<parameters>
<endpoint>#{AppSettings.AuthServiceEndPoint}</endpoint>
</parameters>
</component>
</components>
</castle>
这可能吗?
编辑 (更多信息)
我已经能够从我的 windsor.config 文件(实际上是由城堡 windsor 和自定义 XmlInterpreter 处理的)访问 AppSettings 的能力。
真正的问题是我可以在我的 web.config 中做到这一点吗?
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AuthServiceEndPoint" value="any_old_name_i_like"/>
</appSettings>
<system.web>
...
<system.serviceModel>
<client>
<endpoint
address="net.tcp://localhost/AuthService"
binding="netTcpBinding"
contract="MyServices.Contracts.IAuthService"
name="#{AppSettings.AuthServiceEndPoint}"
bindingConfiguration="netTcpBindingConfig"
/>
</client>
</system.serviceModel>
</configuration>
即 - 我的
<appSettings>
中的访问变量来自我的 web.config 文件的其他部分。
最佳答案
在我的脑海里,我想知道你是否可以用 T4 做到这一点?我在想,也许您可以定义一个模板来解析 Web-Template.config 并输出 Web.config?当然,这只适用于单个文件。
关于asp.net - 在 web.config 中使用 web.config 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3413457/