MVC ASP.net 会话为空

发布于 2024-09-17 20:51:48 字数 860 浏览 5 评论 0原文

我有以下代码,这些代码都可以,直到其他人在网站中添加了一些其他代码,现在有点搞砸了。

这是我的代码:

var existingContext = HttpContext.Current;
var writer = new StringWriter();
var response = new HttpResponse(writer);
var context = new HttpContext(existingContext.Request, response) { User = existingContext.User };
HttpContext.Current = context;
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Default);
HttpContext.Current.Session["Test"] = "test";
for (Int32 i = 0; i < existingContext.Session.Count; i++)
{
    HttpContext.Current.Session.Add(existingContext.Session.Keys[i], existingContext.Session[i]);
}

其背后的想法是能够捕获视图的输出并将其渲染为 pdf。现在我唯一的问题是,当我将上下文分配回 HttpContext.Current 时,会话为空。我需要能够初始化会话,以便我可以向其中分配变量。

我还要补充一点,这是在静态类中 有

public static class ControllerExtensions

任何线索吗?

I have the following code which was ok until someone else put some other code in the site which sorta mucks it up now.

This is my code:

var existingContext = HttpContext.Current;
var writer = new StringWriter();
var response = new HttpResponse(writer);
var context = new HttpContext(existingContext.Request, response) { User = existingContext.User };
HttpContext.Current = context;
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Default);
HttpContext.Current.Session["Test"] = "test";
for (Int32 i = 0; i < existingContext.Session.Count; i++)
{
    HttpContext.Current.Session.Add(existingContext.Session.Keys[i], existingContext.Session[i]);
}

The idea behind this is to be able to capture the output of a view and render it to pdf. Now my only issue is that when i assign context back to HttpContext.Current, the session is null. I need to be able to initialize the session so that i can assign variables into it.

i will also add that this is inside a static class

public static class ControllerExtensions

Any clues?

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

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

发布评论

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

评论(3

幻梦 2024-09-24 20:51:48

如果这种情况发生在 HttpHandler 内部,则需要将 IRequiresSessionState 接口添加到处理程序以使会话可用 -

public class HttpPdfWriteHandler : IHttpHandler, IRequiresSessionState {
     [...]
}

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx

If this is occurring inside of an HttpHandler, you need to add the IRequiresSessionState interface to your handler for the session to be available -

public class HttpPdfWriteHandler : IHttpHandler, IRequiresSessionState {
     [...]
}

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx

习ぎ惯性依靠 2024-09-24 20:51:48

我现在似乎已经解决了这个问题,那就是删除这些行:

            var context = new HttpContext(existingContext.Request, response) { User = existingContext.User };
        HttpContext.Current = context;
        HttpContext.Current.Request.
        for (Int32 i = 0; i < existingContext.Session.Count; i++)
        {
            HttpContext.Current.Session.Add(existingContext.Session.Keys[i], existingContext.Session[i]);
        }

I seem to have solved the issue for now and that was to remove the lines:

            var context = new HttpContext(existingContext.Request, response) { User = existingContext.User };
        HttpContext.Current = context;
        HttpContext.Current.Request.
        for (Int32 i = 0; i < existingContext.Session.Count; i++)
        {
            HttpContext.Current.Session.Add(existingContext.Session.Keys[i], existingContext.Session[i]);
        }
百合的盛世恋 2024-09-24 20:51:48

1)开始——>管理工具 –>服务

2) 右键单击​​ ASP.NET State Service,然后单击“启动”

*此外,您可以将该服务设置为自动,以便它在重新启动后可以工作。

有关更多详细信息,您可以查看我的博客文章: http://jamshidhashimi.com/2011/03/16/unable-to-make-the-session-state-request-to-the-session-state-server/< /a>
参考:
无法向会话状态服务器

1) Start–> Administrative Tools –> Services

2) right click over the ASP.NET State Service and click “start”

*additionally you could set the service to automatic so that it will work after a reboot.

For more details you can check my blog post: http://jamshidhashimi.com/2011/03/16/unable-to-make-the-session-state-request-to-the-session-state-server/
ref:
Unable to make the session state request to the session state server

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