Silve37.Silverlight和ASP.NET相互传参的两种常用方式(QueryString,Cookie)

简介:

  在本节中将讲述Silverlight和ASP.NET页面的相互传参的两种常用方式:Cookie和QueryString。首先我们新建一个名 为SLConnectASP.NET的Silverlight应用程序,然后在SLConnectASP.NET.web项目中添加一个 Index.aspx的页面。

        一、Silverlight和ASPX页面的QueryString传参

        实现思路:在Silverlight端跳转到页面到Index.aspx并且传递一个QueryString参数ID,在该Index.aspx页面显示 出ID。在Index.aspx页面有一个按钮可以向Silverlight所在页面传递一个参数aspxid,在Silverlight端读取 aspxid参数并且显示出来。

        首先我们看Silverlight后台代码:

 
  1. #region QueryString传值 
  2. //QueryString步骤一 
  3. private void button2_Click(object sender, RoutedEventArgs e) 
  4. //传递参数给Asp.net页面 
  5. HtmlPage.Window.Eval("location='" + 
  6. Application.Current.Host.Source.AbsoluteUri.Replace
  7. Application.Current.Host.Source.AbsolutePath, "") + "/index.aspx?id=203';"); 
  8. //QueryString步骤四 
  9. private void ShowQueryString() 
  10. //接收到ASP.NET页面传送过来的QueryString值 
  11. IDictionary<String, String> paras = HtmlPage.Document.QueryString; 
  12. if (paras.ContainsKey("aspxid")) 
  13. this.label1.Content = "获取到ASP.NET传值:" + paras["aspxid"]; 
  14. #endregion 

        然后我们来看Index.aspx.cs的代码如下:

 

 
  1. #region ASP.NET端操作Silverlight传输过来的QueryString值 
  2. //QueryString步骤二 
  3. private void BindQueryString() 
  4. //ASP.NET端获取到Silverlight传输过来的QueryString值 
  5. if (Request.QueryString.Count > 0) 
  6. this.Label1.Text = "获取到的Silverlight客户端QueryString值是:" 
  7. + Request.QueryString["id"].ToString(); 
  8. //QueryString步骤三 
  9. protected void Button1_Click(object sender, EventArgs e) 
  10. //回传QueryString值给Silverlight客户端 
  11. Response.Redirect("./SLConnectASP.NETTestPage.aspx?aspxid=109"); 
  12. #endregion 

        二、Silverlight和ASPX页面的Cookie传参

        实现思路:在Silverlight端创建一个Key名为FirCookieKey,Value值为FirCookieValue的Cookie,然后跳 转页面到Index.aspx页面,在该页面显示出来。在该页面有一个按钮可以修改这个Cookie,并且跳转回Silverlight端,在 Silverlight端读取这个已经被修改过的Cookie并且显示出来。

        首先我们看Silverlight端的后台代码MainPage.xaml.cs:包括步骤一创建Cookie、步骤四读取被修改过的Cookie

 

 
  1. #region Cookie传值 
  2. //Cookie步骤一 
  3. private void button1_Click(object sender, RoutedEventArgs e) 
  4.  
  5. //编写一个键为FirCookieKey,值为FirCookieValue的Cookie 
  6. string oldCookie = HtmlPage.Document.GetProperty("cookie"as String; 
  7. DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(2000); 
  8. string cookie = String.Format("{0}={1};expires={2}""FirCookieKey"
  9. "FirCookieValue", expiration.ToString("R")); 
  10. HtmlPage.Document.SetProperty("cookie", cookie); 
  11. //跳转界面 
  12. HtmlPage.Window.Eval("location='" + 
  13. Application.Current.Host.Source.AbsoluteUri.Replace
  14. Application.Current.Host.Source.AbsolutePath, "") + "/index.aspx';"); 
  15.  
  16. //Cookie步骤四 
  17. private void ShowCookie() 
  18. //显示当前键为FirCookieKey的Cookie值 
  19. String[] cookies = HtmlPage.Document.Cookies.Split(';'); 
  20. foreach (String cookie1 in cookies) 
  21. String[] keyValues = cookie1.Split('='); 
  22. if (keyValues[0] == "FirCookieKey"
  23. this.textBox2.Content = "Cookie的Key值是:" + keyValues[0]; 
  24. this.textBox1.Content = "Cookie的Value值是:" + keyValues[1]; 
  25.  
  26. }; 
  27. #endregion 

         接着我们来看Index.aspx.cs,包括Cookie操作二和Cookie操作三

 

 
  1. #region ASP.NET端获取到操作Cookie值 
  2. //Cookie操作二 
  3. private void BindCookie() 
  4. // ASP.NET端获取到Silverlight传输过来的Cookie值 
  5. HttpCookie cookie = Request.Cookies["FirCookieKey"]; 
  6. if (cookie.Value != null
  7. this.Label2.Text = "获取到的Silverlight客户端Cookie值:" + cookie.Value; 
  8.  
  9. //Cookie操作三 
  10. protected void Button2_Click(object sender, EventArgs e) 
  11. //回传重新设置Cookie值然后传输给Silverlight 
  12. HttpCookie cookie = Request.Cookies["FirCookieKey"]; 
  13. cookie.Value = "NewCookieValue"
  14. HttpContext.Current.Response.Cookies.Add(cookie); 
  15. Response.Redirect("./SLConnectASP.NETTestPage.aspx"); 
  16. #endregion 

        最后这两个实例的MainPage.xaml的代码如下:

 

 
  1. <Grid x:Name="LayoutRoot" Background="White"
  2. <sdk:Label Height="27" HorizontalAlignment="Left" Margin="41,25,0,0" 
  3. Name="label1" VerticalAlignment="Top" Width="284" /> 
  4. <Button Content="传送浏览器参数" Height="38" HorizontalAlignment="Left" 
  5. Margin="41,63,0,0" Name="button2" VerticalAlignment="Top" 
  6. Width="113" Click="button2_Click" /> 
  7.  
  8. <sdk:Label Height="27" HorizontalAlignment="Left" Margin="41,116,0,0" 
  9. Name="textBox2" VerticalAlignment="Top" Width="284" /> 
  10. <sdk:Label Height="27" HorizontalAlignment="Left" Margin="41,149,0,0" 
  11. Name="textBox1" VerticalAlignment="Top" Width="284" /> 
  12. <Button Content="设置Cookie值" Height="38" HorizontalAlignment="Left" 
  13. Margin="41,194,0,0" Name="button1" VerticalAlignment="Top" 
  14. Width="113" Click="button1_Click" /> 
  15. </Grid> 

        Index.aspx的代码如下:

 

 
  1. <div> 
  2.  
  3. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
  4. <br /> 
  5. <br /> 
  6. <asp:Button ID="Button1" runat="server" Text="回传值给Silverlight应用程序QueryString" 
  7. onclick="Button1_Click" /> 
  8.  
  9. <br /> 
  10. <br /> 
  11.  
  12. <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> 
  13. <br /> 
  14. <asp:Button ID="Button2" runat="server" 
  15. Text="回传值给Silverlight应用程序Cookie" onclick="Button2_Click" 
  16. /> 
  17.  
  18. <br /> 
  19.  
  20. </div> 

        本实例采用VS2010+Silverlight 4.0编写。如需源码请点击 SLConnectASP.NET.rar 下载。本实例效果图如下:



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/822718

相关文章
|
5月前
|
存储 开发框架 NoSQL
ASP.NET WEB——项目中Cookie与Session的用法
ASP.NET WEB——项目中Cookie与Session的用法
84 0
|
开发框架 JavaScript .NET
Asp.net C#页面传参的几种方式
Asp.net C#页面传参的几种方式
136 0
.net写入Cookie访问计数器
.net写入Cookie访问计数器
41 0
|
Web App开发 开发框架 安全
[ASP.NET Core 3.1]浏览器嗅探解决部分浏览器丢失Cookie问
看了前文的同学们应该都知道,搜狗、360等浏览器在单点登录中反复重定向,最终失败报错。 原因在于,非Chrome80+浏览器不识别Cookie上的SameSite=none属性值,导致认证Cookie在后续请求中被抛弃。
[ASP.NET Core 3.1]浏览器嗅探解决部分浏览器丢失Cookie问
|
XML 开发框架 负载均衡
抽丝剥茧:浅议ASP.NET Cookie的生成原理
  前言   可能有人知道 Cookie的生成由 machineKey有关, machineKey用于决定 Cookie生成的算法和密钥,并如果使用多台服务器做负载均衡时,必须指定一致的 machineKey用于解密,那么这个过程到底是怎样的呢?   如果需要在 .NETCore中使用 ASP.NETCookie,本文将提到的内容也将是一些必经之路。   抽丝剥茧,一步一步分析   首先用户通过 AccountController->Login进行登录:   //   // POST: /Account/Login   public async Task Login(LoginV
233 0
|
1月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
32 7
|
1月前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
43 0
|
2月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
38 0
|
2月前
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
|
2月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
99 0