gpt4 book ai didi

c# - IRequiresSessionState 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:01 26 4
gpt4 key购买 nike

我已经为此苦苦挣扎了一段时间。

我使用以下代码实现了一个基本的 IHttpHandler,SESSION 不断更新:

namespace ClassLibrary1
{
public class MyHandler : IHttpHandler, IRequiresSessionState
{
public bool IsReusable
{
get
{
return true;
}
}

public void ProcessRequest(HttpContext context)
{
context.Response.Write(context.Session.SessionID + "</br>");
context.Response.Write(DateTime.Now.TimeOfDay.ToString() + "</br>");
}
}
}

如您所见,非常简单的代码。然后我简单地创建一个文件夹 c:\Inetpub\wwwroot\Test。然后我添加一个 bin 文件夹并将我的 dll 放入 bin 文件夹中。

我的 web.config 文件看起来像这样:

<configuration>
<system.webServer>
<handlers>
<add verb="*.x" path="*" name="MyHandler" type="ClassLibrary1.MyHandler" />
</handlers>
</system.webServer>
</configuration>

然后我打开 IIS (IIS 7.0),我必须右键单击默认网站下的测试文件夹,然后单击转换为应用程序。这使它成为一个站点。

然后我转到浏览器并转到 http://localhost/Test/

然后我得到这样的东西: fxnjswtkkzs1silahvpf5xun 09:48:52.9194609

如果我按 F5 或刷新页面, session ID 会更改。这是不应该发生的。

我就是想不通。有趣的是它昨天确实有效。有人可以帮忙....

ps,firefox 和 ie 中的行为相同

提前致谢

最佳答案

是的,您应该每次都获得一个新的SessionID,不仅在您的GenericHandler 上,而且在一个简单的ASPX 页面上除非您访问您的Session 对象。试试这个:

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
object o = context.Session["counter"];
if (o == null)
context.Session["counter"] = 1;
else
context.Session["counter"] = ((int) o) + 1;
context.Response.Write(context.Session.SessionID + "\r\n");
context.Response.Write(context.Session["counter"]);
}

关于c# - IRequiresSessionState 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8110354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com