简体   繁体   English

如何在ASP.NET MVC中的控制器之间传递数据?

[英]how to pass data between to controllers in asp.net mvc?

I'm trying to pass data between controllers using TempData, when I run my project I get below error: 我试图使用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 

for more information about the error and where it originated in the code. 有关错误及其在代码中起源的更多信息。

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

enabled 已启用

here is my web.config: 这是我的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>

It sounds like the server you're trying to run the application on doesn't have the Session State enabled by default. 听起来您要在其上运行应用程序的服务器默认未启用会话状态。

You can enable this on a per application basis through your web.config , simply place this in the file: 您可以通过web.config在每个应用程序的基础上启用它,只需将其放置在文件中即可:

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

This will enable the default in process method of storing session state (server side). 这将启用存储会话状态(服务器端)的默认处理方法。

If that doesn't help, it could point to another issue but we'd need to see the full exception information & stack trace. 如果那没有帮助,则可能指向另一个问题,但我们需要查看完整的异常信息和堆栈跟踪。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 2 个控制器 ASP.NET Core 3.1 MVC 之间传递 ID - Pass ID between 2 controllers ASP.NET Core 3.1 MVC ASP.NET MVC:如何在发布前后的控制器之间传递参数 - ASP.NET MVC: How to pass parameter between controllers before and after post ASP Net MVC 使用全局变量在控制器之间传递数据 - ASP Net MVC Using global variables to pass data between controllers 在控制器之间传递数据的ASP.NET MVC5问题 - ASP.NET MVC5 problem in passing data between Controllers Pro Asp.net core MVC 如何创建视图列表并将数据列表传递给控制器​​(如何将数据列表从视图传递给控制器​​) - Pro Asp.net core MVC how create list of view and pass the data list to the controllers (How can i pass list of data from view to controllers) asp.net mvc 2控制器是如何实例化的? - How are asp.net mvc 2 controllers instantiated? 在ASP.NET MVC控制器之间共享代码 - Sharing code between ASP.NET MVC Controllers 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 如何将数据从视图传递到ASP.NET MVC中的UserControl? - How to pass data from view to UserControl in ASP.NET MVC? 如何将JavaScript数据传递到控制器ASP.NET MVC - How to pass javascript data to controller ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM