ASP.NET 应用程序中的会话值丢失
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人遇到此问题,我想指出发生这种情况是因为我在 ProcessRequest 方法中使用了一些文件操作。
一旦我删除了 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.
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.