如何在asp.net mvc中传递数据到控制器?

时间:2018-05-21 07:16:37

标签: c# asp.net asp.net-mvc session cookies

我正在尝试使用TempData在控制器之间传递数据,当我运行我的项目时,我得到以下错误:

 The SessionStateTempDataProvider class requires session state to be enabled.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace
     

有关错误及其来源的更多信息   代码。

    Exception Details: System.InvalidOperationException: The SessionStateTempDataProvider class requires session state to be
     

启用

这是我的web.config:

  <system.web>
    <customErrors mode="Off">
      <error statusCode="404" redirect="~/View/Shared/NotFountError"/>
      <error statusCode="500" redirect="~/View/Shared/InternalServerError"/>
    </customErrors>
    <authentication mode="None"/>
    <compilation debug="true" targetFramework="4.6.2"/>
    <httpRuntime targetFramework="4.5"/>
  <sessionState mode="InProc" />
  </system.web>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="10.00:00:00"/>
    </staticContent>
    <httpProtocol>
      <customHeaders>
        <remove name="Vary"/>
        <add name="Vary" value="Accept-Encoding"/>
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="FormsAuthenticationModule"/>
      <add name="SA.Filter.Filter" type="SA.Filter.Filter"/>
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
      <remove name="OPTIONSVerbHandler"/>
      <remove name="TRACEVerbHandler"/>
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
    </handlers>
    <directoryBrowse enabled="true"/>
  </system.webServer>

2 个答案:

答案 0 :(得分:0)

这听起来像是您尝试运行应用程序的服务器,默认情况下没有启用会话状态。

您可以通过web.config按应用程序启用此功能,只需将其放在文件中即可:

<system.web>
    ..
    <sessionState mode="InProc" />
</system.web>

这将启用存储会话状态(服务器端)的处理方法中的默认值。

如果这没有帮助,可能会指出另一个问题,但我们需要查看完整的异常信息&amp;堆栈跟踪。

答案 1 :(得分:0)

Google Solved: I added this attribute to the modules node in the web.config and EVERYTHING magically started working:

<modules runAllManagedModulesForAllRequests="true">

It looks like I'm not alone:
Some sample Links are below for your reference.

http://forums.asp.net/p/1293974/2509190.aspx

http://www.flyvergrillen.dk/2009/03/26/being-trapped-in-iis/

I think my pure MVC project (that worked in Test environment) was too simple and may not have forced the MVC framework to require TempData and SessionState, so that's how I'll explain it away ;-)