asp.net-mvc 如何在 ASP.NET MVC 中禁用会话状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/884852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I disable session state in ASP.NET MVC?
提问by Daniel Schaffer
I would like to have a very lightweight ASP.NET MVC site which includes removing as many of the usual HttpModules as possible and disabling session state. However when I try to do this, I get the following error:
我想要一个非常轻量级的 ASP.NET MVC 站点,其中包括尽可能多地删除常用的 HttpModules 并禁用会话状态。但是,当我尝试执行此操作时,出现以下错误:
The SessionStateTempDataProvider requires SessionState to be enabled.
The SessionStateTempDataProvider requires SessionState to be enabled.
I've disabled session state in web.config:
我在 web.config 中禁用了会话状态:
<sessionState mode="Off" />
I understand that ASP.NET MVC uses session state for TempData, but I don't need/want TempData - I just want to disable session state. Help!
我知道 ASP.NET MVC 对 TempData 使用会话状态,但我不需要/想要 TempData - 我只想禁用会话状态。帮助!
采纳答案by Steve Willcock
You could make your own ControllerFactory and DummyTempDataProvider. Something like this:
您可以制作自己的 ControllerFactory 和 DummyTempDataProvider。像这样的东西:
public class NoSessionControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
var controller = base.GetControllerInstance(controllerType);
((Controller) controller).TempDataProvider = new DummyTempDataProvider();
return controller;
}
}
public class DummyTempDataProvider : ITempDataProvider
{
public IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
{
return new Dictionary<string, object>();
}
public void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
{
}
}
And then you would just need to register the controller factory on app startup - e.g. you could do this in global.asax:
然后你只需要在应用程序启动时注册控制器工厂 - 例如你可以在 global.asax 中做到这一点:
ControllerBuilder.Current.SetControllerFactory(new NoSessionControllerFactory());
回答by Daniel Schaffer
I've found one way, which I don't particularly care for:
我找到了一种我并不特别关心的方法:
Create NoTempDataProvider
创建 NoTempDataProvider
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace Facebook.Sites.Desktop.Auth.Models
{
public class NoTempDataProvider : ITempDataProvider
{
#region [ ITempDataProvider Members ]
public IDictionary<String, Object> LoadTempData(ControllerContext controllerContext)
{
return new Dictionary<String, Object>();
}
public void SaveTempData(ControllerContext controllerContext, IDictionary<String, Object> values) { }
#endregion
}
}
Manually Overwrite the Provider in the Controller
手动覆盖控制器中的提供程序
public class AuthController : Controller
{
public AuthController()
{
this.TempDataProvider = new NoTempDataProvider();
}
}
I would greatly prefer a way to do this completely via the configuration, but this works for now.
我非常喜欢一种完全通过配置来做到这一点的方法,但这现在有效。
回答by Haacked
If you need to use TempData for simple strings, you can use the CookieTempDataProvider in MvcFutures http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471.
如果您需要将 TempData 用于简单字符串,您可以使用 MvcFutures http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471 中的 CookieTempDataProvider 。
回答by Raleigh Buckner
回答by ErTR
Modern solution:
现代解决方案:
In ASP.NET, if you do not use the Session object to store any data or if any of the Session events (Session_OnStart or Session_OnEnd) is handled, session state is disabled.
在 ASP.NET 中,如果您不使用 Session 对象来存储任何数据,或者如果处理了任何 Session 事件(Session_OnStart 或 Session_OnEnd),则会话状态将被禁用。
So not using Session (or TempData), disables Session State.
所以不使用会话(或 TempData),禁用会话状态。

