-1

我有 asp 页面,我从查询字符串中获取值并存储在 session 中。 代码是

username = Trim(Request.querystring("username"))
Session("login")=username
NewUserName=Session("login")

现在我想在 Asp.vb Page code is(.aspx page)中访问这个 NewUserName 值

<script type="text/javascript" language="javascript">

var login = '<%= Session["NewUserName"].ToString(); %>';
Session("login")=Login;
alert(login);

</script>

代码是(.aspx.vb)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



Login = Session("login")
    If Session("login") Is Nothing Then
        Response.Redirect("../Default.aspx")
    End If
    Session("qid") = 0
End Sub

但这会产生错误并且无法访问该值。

4

1 回答 1

0

从 asp.net 页面访问内存中的 Classic ASPSESSION 可以做的最简单的桥接是这样的:

  • 在 ASP 端:一个用于输出会话的 asp 页面,称之为 asp2netbridge.asp

    <%
    'Make sure it can be only called from local server '
    if (request.servervariables("LOCAL_ADDR") = request.servervariables("REMOTE_ADDR")) then
        if (Request.QueryString("sessVar") <> "") then
            response.write Session(Request.QueryString("sessVar"))
        end if
    end if
    %>
    
  • 在.net 端,远程调用该asp 页面。:

    private static string GetAspSession(string sessionValue)
     {
        HttpWebRequest _myRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://yourdomain.com/asp2netbridge.asp?sessVar=" + sessionValue));
        _myRequest.ContentType = "text/html";
        _myRequest.Credentials = CredentialCache.DefaultCredentials;
        if (_myRequest.CookieContainer == null)
            _myRequest.CookieContainer = new CookieContainer();
        foreach (string cookieKey in HttpContext.Current.Request.Cookies.Keys)
        {
            ' it is absolutely necessary to pass the ASPSESSIONID cookie or you'll start a new session ! '
            if (cookieKey.StartsWith("ASPSESSIONID")) {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieKey.ToString()];
                _myRequest.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, string.IsNullOrEmpty(cookie.Domain)
                    ? HttpContext.Current.Request.Url.Host
                    : cookie.Domain));
            }
        }
        try
        {
            HttpWebResponse _myWebResponse = (HttpWebResponse)_myRequest.GetResponse();
    
            StreamReader sr = new StreamReader(_myWebResponse.GetResponseStream());
            return sr.ReadToEnd();
        }
        catch (WebException we)
        {
            return we.Message;
        }
    }
    
于 2012-10-24T14:16:02.260 回答