MVC ASP.net 会话为空 [英] MVC ASP.net session is null
问题描述
我有以下代码没问题,直到其他人在网站上放了一些其他代码,现在有点搞砸了.
I have the following code which was ok until someone else put some other code in the site which sorta mucks it up now.
这是我的代码:
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 时,会话为空.我需要能够初始化会话,以便我可以将变量分配给它.
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
有什么线索吗?
推荐答案
我现在似乎已经解决了这个问题,那就是删除这些行:
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]);
}
这篇关于MVC ASP.net 会话为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!