MVC ASP.net 会话为空
我有以下代码,这些代码都可以,直到其他人在网站中添加了一些其他代码,现在有点搞砸了。
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这种情况发生在 HttpHandler 内部,则需要将 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 -
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx
我现在似乎已经解决了这个问题,那就是删除这些行:
I seem to have solved the issue for now and that was to remove the lines:
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