Session state can only be used when enableSessionState is set to true either in a configuration

后端 未结 12 901
[愿得一人]
[愿得一人] 2020-11-29 04:44

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.

Error message image is as be

相关标签:
12条回答
  • 2020-11-29 04:56

    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.

    0 讨论(0)
  • 2020-11-29 04:59

    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 Service service is running? In your screenshot it isn't. It's set to start-up mode Manual which 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.

    Or set the SessionState's mode property to InProc, so that the state service is not required.

     <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.


    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.

    0 讨论(0)
  • 2020-11-29 05:00

    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!!!! ;-)

    0 讨论(0)
  • 2020-11-29 05:05

    Actually jessehouwing gave the solution for normal scenario.

    But in my case I have enabled 2 types of session in my 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.

    0 讨论(0)
  • 2020-11-29 05:06

    also if you are running SharePoint and encounter this error, don't forget to run

    Enable-SPSessionStateService -DefaultProvision
    

    or you will continue to receive the above error message.

    0 讨论(0)
  • 2020-11-29 05:06

    In my case problem went away simply by using HttpContext.Current.Session instead of Session

    0 讨论(0)
提交回复
热议问题