ASP.NET 应用程序中的会话值丢失

发布于 2024-10-21 07:52:37 字数 1949 浏览 5 评论 0原文

我在 ASP.NET 中创建了 HttpHandler 类,并配置了一个网站来处理带有 *.test 路径的任何请求。

public class GameHandler : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public void ProcessRequest (HttpContext context)
    {
        if (context.Request.ContentType == "application/json; charset=utf-8")
        {
            ...
            switch (parameters ["type"])
            {
                case "Setup":
                    result = Setup (context);

                    break;

                case "DoStep":
                    result = DoStep (context, parameters);

                    break;
             }
             ...
         }
         else
             context.Response.Write (@"
                  <html>
                      <head>
                      </head>
                      <body>
                          <!-- some HTML -->
                      </body>
                  </html>"); // this is returned on first request 
    }

在Setup方法中,我有这样的代码:

context.Session ["Game"] = new Game ();

然而,在DoStep方法中,context.Session.Count = 0并且context.Session[“Game”]为NULL。 在客户端我使用jquery来调用这些函数。这样的调用如下所示:

 $.ajax({
        url: "/test.test",  
        type: "POST",
        data: "{'type':'Setup'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {...}
 });

 $.ajax({
        url: "/test.test",  
        type: "POST",
        data: "{'type':'DoStep','row':'" + row + "','column':'" + column + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {...}
 });

我怀疑问题是 ASP.NET 不知道从 javascript 发送的这些请求属于同一个会话,这就是会话值丢失的原因。我认为我需要发回一些 cookie 信息或其他信息以便识别下一个请求,但事实是我不知道。

非常感谢任何帮助。

I created HttpHandler class in ASP.NET and configured a website to handle any request with the *.test path.

public class GameHandler : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public void ProcessRequest (HttpContext context)
    {
        if (context.Request.ContentType == "application/json; charset=utf-8")
        {
            ...
            switch (parameters ["type"])
            {
                case "Setup":
                    result = Setup (context);

                    break;

                case "DoStep":
                    result = DoStep (context, parameters);

                    break;
             }
             ...
         }
         else
             context.Response.Write (@"
                  <html>
                      <head>
                      </head>
                      <body>
                          <!-- some HTML -->
                      </body>
                  </html>"); // this is returned on first request 
    }

In the Setup method I have such a code:

context.Session ["Game"] = new Game ();

In the DoStep method however, context.Session.Count = 0 and context.Session["Game"] is NULL.
In client side I use jquery to call these functions. Such a call looks like this:

 $.ajax({
        url: "/test.test",  
        type: "POST",
        data: "{'type':'Setup'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {...}
 });

 $.ajax({
        url: "/test.test",  
        type: "POST",
        data: "{'type':'DoStep','row':'" + row + "','column':'" + column + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {...}
 });

I suspect the problem is that ASP.NET doesn't know these requests sent from javascript belong to the same session and that's why the Session values are lost. I think that I would need to send back some cookie information or something for the next request to be identified but the fact is I don't have any idea.

Any help is really appreciated.

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

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

发布评论

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

评论(1

2024-10-28 07:52:37

如果有人遇到此问题,我想指出发生这种情况是因为我在 ProcessRequest 方法中使用了一些文件操作。

var doc = XDocument.Load (@"c:\XO_Game_Website\bin\test.xml"); 
if (context.Session ["something"] == null) 
{ 
    context.Session.Add("something", "something"); 
    doc.Root.Element ("xxx").Value = "null"; 
} 
else 
{
    doc.Root.Element ("xxx").Value = "not null";
}
doc.Save("path"); 

一旦我删除了 doc.Save("path");从方法来看,一切都很好。尽管如此,我还是不明白为什么文件操作代码会重置会话值。

快乐编码。

In case anyone encounters this problem I would like to specify that this happened because I use some file manipulation in the ProcessRequest method.

var doc = XDocument.Load (@"c:\XO_Game_Website\bin\test.xml"); 
if (context.Session ["something"] == null) 
{ 
    context.Session.Add("something", "something"); 
    doc.Root.Element ("xxx").Value = "null"; 
} 
else 
{
    doc.Root.Element ("xxx").Value = "not null";
}
doc.Save("path"); 

Once I removed the doc.Save("path"); from the method, everything just worked fine. Nonetheless I can't figure out why on earth would the file manipulation code reset the session values.

Happy coding.

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