.net 会话状态只能在配置中的 enableSessionState 设置为 true 时使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14334147/
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
Session state can only be used when enableSessionState is set to true either in a configuration
提问by Sampath
I am working on Asp.net MVC 2 app with c# by using vs 2010.I am having below mentioned error when I run my app locally under debug mode.
我正在使用 vs 2010 使用 c# 开发 Asp.net MVC 2 应用程序。当我在调试模式下本地运行我的应用程序时,我遇到了下面提到的错误。
Error message image is as below :
错误信息图片如下:


Error message text is as below :
错误信息文本如下:
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the
<configuration>\<system.web>\<httpModules>section in the application configuration.
会话状态只能在 enableSessionState 设置为 true 时使用,无论是在配置文件中还是在 Page 指令中。还请确保 System.Web.SessionStateModule 或自定义会话状态模块包含在
<configuration>\<system.web>\<httpModules>应用程序配置的部分中。
What I did for sort out this issue are as below :
我为解决这个问题所做的如下:
Try 1 : web.config file has been changed like below:
尝试 1:web.config 文件已更改如下:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer
Try 2 is as below :
尝试 2 如下:


But Unfortunately still I am having same issue which I have mentioned Above.
但不幸的是,我仍然遇到上面提到的相同问题。
UPDATE
更新


Do you have any solution for this ?
你有什么解决办法吗?
回答by jessehouwing
Did you enable the session state in the section as well?
您是否也启用了该部分中的会话状态?
<system.web>
<pages enableSessionState="true" />
</system.web>
Or did you add this to the page?
或者你把它添加到页面上了吗?
<%@Page enableSessionState="true">
And did you verify that the ASP.NET Session State Manager Serviceservice is running? In your screenshot it isn't. It's set to start-up mode Manualwhich requires you to start it every time you want to make use of it. To start it, highlight the service and click the green play button on the toolbar. To start it automatically, edit the properties and adjust the Start-up type.
您是否验证了该ASP.NET Session State Manager Service服务正在运行?在您的屏幕截图中,它不是。它设置为启动模式Manual,要求您每次使用它时都启动它。要启动它,请突出显示该服务并单击工具栏上的绿色播放按钮。要自动启动,请编辑属性并调整启动类型。
Or set the SessionState's mode property to InProc, so that the state service is not required.
或者将 SessionState 的 mode 属性设置为InProc,这样就不需要状态服务了。
<system.web>
<sessionState mode="InProc" />
</system.web>
Check MSDN for all the options available to you with regards to storing data in the ASP.NET session state.
检查MSDN,了解有关在 ASP.NET 会话状态中存储数据的所有可用选项。
Note: It's better to have your Controller fetch the value from the session and have it add the value to the Model for your page/control (or to the ViewBag), that way the View doesn't depend on the HttpSession and you can choose a different source for this item later with minimal code changes. Or even better, not use the session state at all. It can kill you performance when you're using a lot of async javascript calls.
注意:最好让您的控制器从会话中获取值并将其添加到您的页面/控件(或 ViewBag)的模型中,这样视图不依赖于 HttpSession,您可以选择稍后以最少的代码更改此项目的不同来源。或者甚至更好,根本不使用会话状态。当您使用大量异步 javascript 调用时,它会降低您的性能。
回答by Nacht - Reinstate Monica
also if you are running SharePoint and encounter this error, don't forget to run
此外,如果您正在运行 SharePoint 并遇到此错误,请不要忘记运行
Enable-SPSessionStateService -DefaultProvision
or you will continue to receive the above error message.
否则您将继续收到上述错误信息。
回答by Sampath
Actually jessehouwinggave the solution for normal scenario.
实际上jessehouwing给出了正常场景的解决方案。
But in my case I have enabled 2 types of session in my web.config file
但在我的情况下,我在我的 web.config file
It's like below.
就像下面这样。
First one :
第一 :
<modules runAllManagedModulesForAllRequests="true">
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
Second one :
第二个 :
<sessionState mode="Custom" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="PawLoyalty" applicationName="PawLoyalty"/>
</providers>
</sessionState>
So in my case I have to comment Second one.B'cos that thing for the production.When I commented out second one my problem vanished.
所以在我的情况下,我必须评论第二个。B'cos 那个东西用于生产。当我评论第二个时,我的问题消失了。
回答by Michelle Sollicito
I want to let everyone know that sometimes this error just is a result of some weird memory error. Restart your pc and go back into visual studio and it will be gone!! Bizarre! Try that before you start playing around with your web config file etc like I did!!!! ;-)
我想让大家知道,有时这个错误只是一些奇怪的内存错误的结果。重新启动你的电脑并回到visual studio,它就会消失!!奇怪!在你开始像我一样使用你的 web 配置文件等之前尝试一下!!!!;-)
回答by Samara
In my case problem went away simply by using HttpContext.Current.Sessioninstead of Session
在我的情况下,问题只是通过使用HttpContext.Current.Session而不是Session
回答by MAFAIZ
For SharePointCan find the web config file in C:\inetpub\wwwroot\wss\VirtualDirectories\Sitecollection port number - and Make changes
对于 SharePoint可以在 C:\inetpub\wwwroot\wss\VirtualDirectories\Sitecollection 端口号中找到 web 配置文件 - 并进行更改
<system.web>
<pages enableSessionState="true" />
</system.web>
and using SharePoint Management Shell Run below Command
并使用 SharePoint 命令行管理程序运行以下命令
Enable-SPSessionStateService -DefaultProvision
回答by Dan Diplo
I realise there is an accepted answer, but this may help people. I found I had a similar problem with a ASP.NET site using NET 3.5 framework when running it in Visual Studio 2012 using IIS Express 8. I'd tried all of the above solutions and none worked - in the end running the solution in the in-built VS 2012 webserver worked. Not sure why, but I suspect it was a link between 3.5 framework and IIS 8.
我意识到有一个可以接受的答案,但这可能会对人们有所帮助。我发现使用 NET 3.5 框架的 ASP.NET 站点在使用 IIS Express 8 在 Visual Studio 2012 中运行时遇到了类似的问题。我尝试了上述所有解决方案,但都没有奏效 - 最后在内置 VS 2012 网络服务器工作。不知道为什么,但我怀疑这是 3.5 框架和 IIS 8 之间的链接。
回答by Sebris87
This error was raised for me because of an unhandled exception thrown in the Public Sub New()(Visual Basic) constructor function of the Web Page in the code behind.
由于Public Sub New()在后面的代码中网页的(Visual Basic)构造函数中抛出了未处理的异常,因此为我引发了此错误。
If you implement the constructor function wrap the code in a Try/Catch statement and see if it solves the problem.
如果您实现构造函数,请将代码包装在 Try/Catch 语句中,看看它是否解决了问题。
回答by Adeem
Following answer from below given path worked fine.
I found a solution that worked perfectly! Add the following to web.config:
我找到了一个完美的解决方案!将以下内容添加到 web.config:
<system.webServer>
<modules>
<!-- UrlRewriter code here -->
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />
</modules>
</system.webServer>
Hope this helps someone else!
希望这对其他人有帮助!
回答by HydPhani
- Could be your skype intercepting your requests at 80 port, in Skype options uncheck
- Or Your IE has connection checked for Proxy when there is no proxy
- Or your fiddler could intercept and act as proxy, uncheck it!
- 可能是您的 Skype 在 80 端口拦截您的请求,在 Skype 选项中取消选中
- 或者当没有代理时,您的 IE 已检查代理的连接
- 或者您的提琴手可以拦截并充当代理,取消选中它!
Solves the above problem, It solved mine!
解决了上面的问题,它解决了我的!
HydTechie
海德科技

