会话值未存储在ASP.NET应用程序中

问题描述:

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

I created an 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
    }



Setup方法中,我有这样的代码:
context.Session ["Game"] = new Game ();
但是,在DoStep方法中,context.Session.Count = 0context.Session["Game"]为NULL.我发现context.Application也会丢失我放入其中的值.在客户端,我使用jquery来调用这些函数.这样的呼叫看起来像这样:



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. I figured out that context.Application also loses the values that I put in it. 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) {...}
});



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

非常感谢您的帮助.



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.

.ajax({ 网址:"/test.test", 输入:"POST", 数据:"{" type:" Setup}", contentType:"application/json; charset = utf-8", dataType:"json", 成功:功能(结果){...} });
.ajax({ url: "/test.test", type: "POST", data: "{''type'':''Setup''}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) {...} });


.ajax({ 网址:"/test.test", 输入:"POST", 数据:"{" type:" DoStep," row:""+行+","列:""+列+"''}, contentType:"application/json; charset = utf-8", dataType:"json", 成功:功能(结果){...} });
.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发送的这些请求属于同一会话,这就是Session值丢失的原因.我认为我需要发送一些cookie信息或其他信息,以便下次请求被识别,但事实是我没有任何想法.

真的很感谢您的帮助.



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.


您好,

在执行ajax请求之前,是否访问ASP.NET页(从与服务正在运行的同一ASP.NET应用程序访问)?

如果是,请在交付页面之前尝试在会话中保存任何内容(例如,userId/GUID,或者如果不需要任何有用的数据,请保存当前的DateTime).这应该使会话ID永久可用,并且可用于ajax调用/服务.

希望这会有所帮助:)

致以最诚挚的问候,祝您有愉快的一天,
停止
Hello,

Do you access a ASP.NET Page (from the same ASP.NET application as the service is working on) before you execute the ajax requests?

If yes, try to save anything in the session before you deliver the page (for example a userId / GUID or if you do not need any useful data, save the current DateTime). This should make the session id permanent and usable for the ajax calls / services.

Hope this helps :)

Best regards and have a nice day,
Stops