RKM*_*RKM 9 c# silverlight web-config
我试图在Silverlight中使用我的Web配置文件.
我在web.config中添加了以下内容:
<configuration>
<appSettings>
<add key="FileHeader" value="file://***.com/Builds/"/>
<add key="WebHeader" value="http://***.com/dev/builds"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我想尝试使用它们
string temp= System.Configuration!System.Configuration.ConfigurationManager.AppSettings.Get("FileHeader");
Run Code Online (Sandbox Code Playgroud)
但是它不起作用,它给出了一个错误"只有赋值,调用,递增,递减......才能用作语句"
slf*_*fan 19
您无法从Silverlight应用程序中读取web.config,因为Silverlight应用程序在客户端(在浏览器中)而不是在服务器上运行.
从您的服务器代码,您可以访问应用程序设置
string temp = Configuration.ConfigurationManager.AppSettings["FileHeader"];
Run Code Online (Sandbox Code Playgroud)
但你必须把它们发送给客户.你可以通过使用InitParams来做到这一点
<param name="initParams" value="param1=value1,param2=value2" />
Run Code Online (Sandbox Code Playgroud)
在您的服务器代码(Default.aspx的Page_Load)中,您可以循环遍历所有AppSettings并动态创建initParams的值.
在Silverlight应用程序中,您可以访问Application_Startup事件中的参数:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
if (e.InitParams.ContainsKey("param1"))
var p1 = e.InitParams["param1"];
}
Run Code Online (Sandbox Code Playgroud)
或循环遍历所有参数并将它们存储在配置字典中.像这样,您可以在客户端的Silverlight应用程序中设置应用程序.
您无法从Silverlight应用程序中读取web.config,因为SL .NET Framework中不存在配置命名空间,但您可以执行以下操作:
public static string GetSomeSetting(string settingName)
{
var valueToGet = string.Empty;
var reader = XmlReader.Create("XMLFileInYourRoot.Config");
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")
{
if (reader.HasAttributes)
{
valueToGet = reader.GetAttribute("key");
if (!string.IsNullOrEmpty(valueToGet) && valueToGet == setting)
{
valueToGet = reader.GetAttribute("value");
return valueToGet;
}
}
}
}
return valueToGet;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16351 次 |
| 最近记录: |