silverlight 获取路径 config

时间:2021-12-04 09:43:44

1.获取web.config配置内容:

web.config

default.aspx

protected string InitParams { get; set; }
InitParams = string.Format("{0}={1}", "key1", "value1");
InitParams += string.Format(",{0}={1}", "Token", WebConfigurationManager.AppSettings["Token"]);

" />

App.xaml

private void Application_Startup(object sender, StartupEventArgs e)

{
  string token= e.InitParams["Token"];
  ...................

}

2.获取查询字符串

if (HtmlPage.Document.QueryString.ContainsKey("Token"))
{
string param = HtmlPage.Document.QueryString["Token"];//大小写区分?
}

==============================================================================================
本文将建立一个silverlight项目中读取宿主网站web.config配置文件数据的简单实例,以下是详细步骤:

silverlight程序会被下载到客户端去执行,所以没法操作到服务端的配置文件,导致了我们在部署时遇到很多问题,(例如:silverlight程序和wcf的通讯地址,在发布时,我们的开发环境配置将可能不再适用,需要根据服务端实际情况重新配置),如果可以让silverlight程序读取到web.config中的配置数据,将会大大简化我们的部署工作,那么有没有办法可以达到这样的效果呢,答案是肯定的。

1.首先在宿主网站的web.config中,添加我们要传递给silverlight程序的键值对

复制代码

复制代码
本例中添加了一个[wcfServiceAddress]的值

2.打开vs为我们在宿主网站中自动生成的访问页面(*.aspx,*.html),找到已经自动添加的一些键值对如下

复制代码

silverlight 获取路径 config

复制代码
那么就在这里添加一条我们的键值对吧,键为InitParams,值为我们先前添加在web.config中的内容

' />
3.宿主网站的修改完毕,接下来修改一下我们的silverlight程序,打开对应的App.xml.cs,在启动事件里添加获取我们的键值对,并添加到程序资源中去,方便我们的使用

复制代码
private void Application_Startup(object sender, StartupEventArgs e)
{
//将读取到的WCF地址保存到资源中。
var slServicePath = e.InitParams["WcfServiceAddress"];
Application.Current.Resources.Add("WcfServiceAddress", slServicePath);

this.RootVisual = new MainPage();
}
复制代码
ok,new一个页面来做舞台show一下效果吧

前端:

复制代码

复制代码
后台:

this.ConfigText.Text = Application.Current.Resources["WcfServiceAddress"] as string;