服务器变量无法从我的 ASP 页面传输到我的 Silverlight 应用程序

发布于 2025-01-05 18:51:58 字数 651 浏览 0 评论 0原文

我有一个像这样的基本 .ASP 页面,当点击该页面时,会在向会话变量添加值后将用户重定向到我的 SL 应用程序

<!-- Default.htm -->
<html>
<%Session("valuekey")=somevalue%>
<Head>
<META http-equiv="Refresh" content="0; URL=/appdirectory/myapp.aspx?lsv=true"></HEAD>
<Body></Body>
</HTML>

当我访问 myapp.aspx 上托管的 SL 应用程序时,第一个认为它会检查lsv 查询字符串。如果它等于 true,它将调用 WCF 服务,其代码如下:

object x = HttpContext.Current.Session["valuekey"];
if(x == null)
{
ServiceError.Message = "No session variable found";
}
else
{
return x.ToString();
}

有谁知道为什么当我的 SL 应用程序尝试获取会话变量时,在重定向之前我刚刚在 ASP 页面上添加的会话变量不再存在?

I have a basic .ASP page like this, which when hit redirects the user to my SL app after adding a value to the session variables

<!-- Default.htm -->
<html>
<%Session("valuekey")=somevalue%>
<Head>
<META http-equiv="Refresh" content="0; URL=/appdirectory/myapp.aspx?lsv=true"></HEAD>
<Body></Body>
</HTML>

When I get to my SL app hosted on myapp.aspx, the first think it does it check for the lsv QueryString. If it equals true, it calls a WCF service with code like

object x = HttpContext.Current.Session["valuekey"];
if(x == null)
{
ServiceError.Message = "No session variable found";
}
else
{
return x.ToString();
}

Does anyone know why the session variable that I just added on the ASP page before the redirect no longer exists when my SL app tried to fetch it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩套路吗 2025-01-12 18:51:58

这个答案假设 ASP classic 首先进入这个方程是有充分理由的。也许是因为 Silverlight 被引入到现有的 ASP 站点中。问题是大多数 Silverlight 客户端-服务器示例都涉及服务器上的 .NET WCF。

您问题的答案是不要使用 WCF 服务来获取会话数据。请改用简单的 ASP 页面。使用简单的 XML 结构将所需的会话数据传送到 Silverlight 应用程序应该相当简单。使用可用于将 XML 反序列化为简单类的 DTO 类。像这样的东西:(

警告:空中代码)

 <%
     Dim dom:  Set dom = CreateObject("MSXML2.DOMDocument.3.0")
     dom.loadXML "<SessionData />" 

     AddElem dom.documentElement, "ValueKey", Session("valuekey")
     AddElem dom.documentElement, "SomeOtherValue", Session("othervalue")
     ''# include other session values needed by client here.

     Response.ContentType = "text/xml"
     Response.CharSet = "utf-8"
     dom.save Response

     Sub AddElem(parent, name, value)
         Dim elem: Set elem = parent.ownerDocument.createElement(name)
         parent.appendChild elem
         elem.text = value;
     End Sub
 %>

在 Silverlight 中:

 [DataContract]
 public class SessionData
 {
     [DataMember(Order=1)]
     public string ValueKey {get; set; }

     [DataMember(Order=2)]
     public string SomeOtherValue {get; set; }

     public static void Fetch(Action<string> returnResult, Action<exception> fail)
     {
         WebClient client = new WebClient();
         OpenReadCompletedEventHandler eh = null;
         eh = (s, args) =>
         {
             try
             {
                 var sr = new DataControlSerializer(typeof(SessionData));
                 returnResult((SessionData)sr.ReadObject(args.Result)); 
             }
             catch (Exception e)
             {
                 fail(e);
             }
             finally
             {
                client.OpenReadAsyncCompleted -= eh;
             }
          };
          client.OpenReadAsyncCompleted += eh;
          client.OpenReadAsync(new Uri("../serviceFolder/sessionState.asp", UriKind.Relative));
     }

 }

现在在某些 UI 或 ViewModel 中,您可以

     void SessionData_Available(SessionData sessionData)
     {
          _sessionData = sessionData;
          // Other actions needed once session data has arrived.
     }

     void ReportProblem(Exception e)
     {
          // Some UI change to inform user of failed fetch
     }

...

     SessionData.Fetch(SessionData_Available, ReportProblem);

This answer assumes there is a good reason why ASP classic enters into the equation in the first place. Perhaps its because Silverlight is being introduced into an existing ASP site. The problem is that most Silverlight Client-Server examples involve .NET WCF on the server.

The answer to your problem is don't use a WCF service to fetch your session data. Use a simple ASP page instead. It should be fairly straight-forward to use a simple XML structure to carry the session data you want to the Silverlight app. Use a DTO class that can be used to deserialise the XML into a simple class. Something like this:

(Caveat: air code)

 <%
     Dim dom:  Set dom = CreateObject("MSXML2.DOMDocument.3.0")
     dom.loadXML "<SessionData />" 

     AddElem dom.documentElement, "ValueKey", Session("valuekey")
     AddElem dom.documentElement, "SomeOtherValue", Session("othervalue")
     ''# include other session values needed by client here.

     Response.ContentType = "text/xml"
     Response.CharSet = "utf-8"
     dom.save Response

     Sub AddElem(parent, name, value)
         Dim elem: Set elem = parent.ownerDocument.createElement(name)
         parent.appendChild elem
         elem.text = value;
     End Sub
 %>

In Silverlight:

 [DataContract]
 public class SessionData
 {
     [DataMember(Order=1)]
     public string ValueKey {get; set; }

     [DataMember(Order=2)]
     public string SomeOtherValue {get; set; }

     public static void Fetch(Action<string> returnResult, Action<exception> fail)
     {
         WebClient client = new WebClient();
         OpenReadCompletedEventHandler eh = null;
         eh = (s, args) =>
         {
             try
             {
                 var sr = new DataControlSerializer(typeof(SessionData));
                 returnResult((SessionData)sr.ReadObject(args.Result)); 
             }
             catch (Exception e)
             {
                 fail(e);
             }
             finally
             {
                client.OpenReadAsyncCompleted -= eh;
             }
          };
          client.OpenReadAsyncCompleted += eh;
          client.OpenReadAsync(new Uri("../serviceFolder/sessionState.asp", UriKind.Relative));
     }

 }

Now in some UI or ViewModel you do

     void SessionData_Available(SessionData sessionData)
     {
          _sessionData = sessionData;
          // Other actions needed once session data has arrived.
     }

     void ReportProblem(Exception e)
     {
          // Some UI change to inform user of failed fetch
     }

...

     SessionData.Fetch(SessionData_Available, ReportProblem);
清浅ˋ旧时光 2025-01-12 18:51:58

Silverlight 无法使用您的 asp.net 会话变量。他们只存在于服务器中。

检查这个简单的解决方法。它可能对你有帮助。

Your asp.net session variables are not available to Silverlight. They live in the server only.

Check this simple workaround. It might help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文