Silve37.SilverlightとASP.NET相互参照の2つの一般的な方法(QueryString,Cookie)


この節ではSilverlightとASPについて述べる.NETページの相互参照の2つの一般的な方法:CookieとQueryString.まずSLConnectASPという名前を新規作成します.NETのSilverlightアプリ、そしてSLConnectASP.NET.WebプロジェクトにIndexを追加します.aspxのページ.
一、SilverlightとASPXページのQueryString伝参
実現構想:Silverlight端でページにジャンプしてIndexに移動する.Aspxは、IndexにおけるQueryStringパラメータIDを渡す.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側にFirCookieKeyというKeyを作成し、Value値がFirCookieValueのCookieを作成し、Indexにページをジャンプする.aspxページは、このページに表示されます.このページには、このCookieを修正し、Silverlight側にジャンプし、Silverlight側で修正されたCookieを読み取り、表示するボタンがあります.
まずSilverlight側のバックグラウンドコードMainPageを見てみましょう.xaml.cs:ステップ1でCookieを作成し、ステップ4で修正された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操作2とCookie操作3を含む
 

  
  
  
  
  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 

最後の2つの例の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> 

この例はVS 2010+Silverlight 4.0で記述されています.ソースコードが必要な場合はSLConnectASPをクリックしてください.NET.rarダウンロード.この例の効果図は次のとおりです.