调用页面方法时未保留会话状态

发布于 2024-07-29 04:12:31 字数 779 浏览 2 评论 0原文

我的服务器端代码:

    [WebMethod(CacheDuration = 0, EnableSession = true)]
    public static int UserID()
    {
        if (HttpContext.Current.Session["UserID"] == null) return 0;
        int UserID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
        return (UserID);

    }

我的客户端代码:

$.ajax({
    type: "POST", cache: false,
    url: "Login.aspx/UserID",
    data: "{'r':" + rnd() + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg);
    }
});

该代码在我的本地主机上运行良好。 和“UserID”ajax 调用,返回 Session 参数的正确值。

但是当我尝试在服务器上上传我的网站时,“UserID”ajax 调用总是返回 false!

我的服务器是asp.net 2.0,我使用jquery 1.3.2

所以请帮助解决这个问题。

My server side code:

    [WebMethod(CacheDuration = 0, EnableSession = true)]
    public static int UserID()
    {
        if (HttpContext.Current.Session["UserID"] == null) return 0;
        int UserID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
        return (UserID);

    }

My Client side code:

$.ajax({
    type: "POST", cache: false,
    url: "Login.aspx/UserID",
    data: "{'r':" + rnd() + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg);
    }
});

This codes runs well on my localhost.
and the "UserID" ajax call, return the right value of the Session parameter.

but when i try to upload my website on the server, the "UserID" ajax call always returns false!!

my server is asp.net 2.0 and I'm using jquery 1.3.2

So please help to solve this problem.

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

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

发布评论

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

评论(2

怎言笑 2024-08-05 04:12:31

阅读本文:ASP.NET 会话状态常见问题解答
我在这个问题中找到了答案:

  1. 问:会话状态在某些 Web 服务器上有效,但在其他服务器上无效。
    A:可能是机器名问题。 请参阅 http://support.microsoft.com/default.aspx ?scid=kb;EN-US;q316112
  2. 问:为什么使用 InProc 模式时我的 Session 变量经常丢失?
    A:可能是因为应用程序回收。 请参阅 http://support.microsoft.com/default.aspx ?scid=kb;en-us;Q316148

将 sessionState 模式更改为“StateServer”解决了该问题。
使用下面的代码:

<sessionState mode="StateServer"
  stateConnectionString="tcpip=localhost:42424"
  cookieless="false"
  timeout="999"/>

Read this article: ASP.NET Session State FAQ
I found my answer in this questions:

  1. Q: Session states works on some web servers but not on others.
    A: Maybe machine name problem. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q316112 .
  2. Q: Why are my Session variables lost frequently when using InProc mode?
    A: Probably because of application recycle. See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316148

Changing sessionState mode to "StateServer" solved the problem.
Use code below:

<sessionState mode="StateServer"
  stateConnectionString="tcpip=localhost:42424"
  cookieless="false"
  timeout="999"/>
顾北清歌寒 2024-08-05 04:12:31

有几点需要检查:

您是否使用 WebFarm 配置? 在 WebFarm 配置中使用会话时可能会出现问题。

您是否使用 Fiddler 或 FireBug 等工具检查过网络流量? AJAX 调用期间是否发送 ASP.NET Auth cookie?

您能确认您的会话变量有效吗?

A few things to check :

Are you using WebFarm configuration? there might be a problem when using session in WebFarm configuration.

Have you check your network traffic using tools like Fiddler or FireBug? Is ASP.NET Auth cookie sent during AJAX call?

Can you confirm your session var is valid?

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