我想有一个非常轻量级的ASP.NET MVC站点,其中包括尽可能多地删除常用的HttpModule并禁用会话状态.但是,当我尝试这样做时,我收到以下错误:
The SessionStateTempDataProvider requires SessionState to be enabled.
我在web.config中禁用了会话状态:
<sessionState mode="Off" />
Run Code Online (Sandbox Code Playgroud)
我知道ASP.NET MVC使用TempData的会话状态,但我不需要/想要TempData - 我只是想禁用会话状态.救命!
它没有明确地写在某处,但在阅读了几篇关于ASP.NET MVC的博客之后,我感觉如此.只是好奇并想到这里问它.
更新:
我不是在询问服务器上的内存/存储/ RAM问题.对于他们来说,存在一个解决方案来存储会话进程.我知道.我很好奇,有没有我们不得不在WebForms中使用Session的情况,但我们现在可以在MVC中避免使用MVC提供的漂亮的结构化方式吗?
ASP.NET团队建议使用缓存而不是会话,我们在过去几年中停止使用会话来处理WebForm模型.所以我们通常在web.config中关闭会话
<sessionState mode="Off" />
Run Code Online (Sandbox Code Playgroud)
但是,现在当我使用此设置测试ASP.NET MVC应用程序时,它会SessionStateTempDataProvider在mvc框架内的类中抛出错误,它要求我打开会话状态,我做了并且它有效.查看它使用会话的来源:
// line 20 in SessionStateTempDataProvider.cs
Dictionary<string, object> tempDataDictionary =
httpContext.Session[TempDataSessionStateKey] as Dictionary<string, object>;
Run Code Online (Sandbox Code Playgroud)
那么,为什么他们会在这里使用会话?我错过了什么?
================================================== ======
编辑对不起这篇文章不是故意讨论会话与缓存,而是在ASP.NET MVC的上下文中,我只是想知道为什么会话在这里使用.在这篇博文中,Scott Watermasysk也提到关闭会话是一个很好的做法,所以我只是想知道为什么我必须从这里开启使用MVC.
我有这个代码
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
// some lines of code . bla bla bla
TempData["loginModel"] = loginModel;
return RedirectToAction("index", "premium");
}
...
}
Run Code Online (Sandbox Code Playgroud)
这个控制器在这里
public ActionResult Index()
{
var loginModel = TempData["loginModel"] as LoginModel;
...
}
Run Code Online (Sandbox Code Playgroud)
现在,当页面加载时,一切似乎都运行正常.但是当我刷新时,一切都搞砸了,它说loginModel就像是null.问题是,我怎么能跟踪当前的登录用户.我启用了表单身份验证.TNX
错误如下
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it …Run Code Online (Sandbox Code Playgroud)