简体   繁体   English

你需要做些什么才能让会话在MVC5中运行?

[英]What do you have to do to get sessions working in MVC5?

What do you have to do to get sessions working in MVC5? 你需要做些什么才能让会话在MVC5中运行? In MVC4 I can create a new project and add 在MVC4中,我可以创建一个新项目并添加

Session["TEST"] = "worked";

to a controller and it works without any issues. 到一个控制器,它没有任何问题。 If I do that in an MVC5 project it works fine in Visual Studio 2012, but when I deploy it to IIS I get a "System.NullReferenceException" exception. 如果我在MVC5项目中这样做,它在Visual Studio 2012中工作正常,但是当我将它部署到IIS时,我得到一个“System.NullReferenceException”异常。

Updated Information: 更新信息:

I have MVC5 running in VS 2012 without issue. 我有没有问题在VS 2012中运行MVC5。 I installed all the updates to get that working and have everything working except for sessions. 我安装了所有更新以使其正常工作,除会话外一切正常。 I don't think that MVC5 is the issue, I think it's something about the project template that differs from the MVC4 template. 我不认为MVC5是问题,我认为这是与MVC4模板不同的项目模板。

Here is my controller action that's causing the issue: 这是导致问题的我的控制器操作:

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template.";
        Session["TEST"] = "worked";
        return View();
    }

Potential fix: 可能的修复:

Adding runAllManagedModulesForAllRequests="true" to my web.config fixes the issue. runAllManagedModulesForAllRequests =“true”添加到我的web.config修复了该问题。

<modules runAllManagedModulesForAllRequests="true">

I can also do the suggested answer here: 我也可以在这里做建议的答案:

Asp.net MVC error with configured managed modules 配置托管模块的Asp.net MVC错误

<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />

And that fixes my issue as well. 这也解决了我的问题。 Can anyone provide me some further reading on which one is a better solution and why? 任何人都可以为我提供一个更好的解决方案,为什么?

The MVC 5 straight forward is not supported in Visual Studio 2012. Visual Studio 2012不支持MVC 5直接版。

You need to install ASP.NET and Web Tools 2013.1 for Visual Studio 2012 for MVC 5 to work in VS 2012, it has added support for ASP.NET MVC 5 and Web API 2 Templates for vs 2012. 您需要为Visual Studio 2012 for MVC 5安装ASP.NET和Web Tools 2013.1才能在VS 2012中工作,它已为vsm 2012添加了对ASP.NET MVC 5和Web API 2模板的支持。

Besides that, I don't think " Session["TEST"] = "worked"; doesn't work" has to do anything with MVC 5. 除此之外,我不认为“ Session["TEST"] = "worked";不起作用”必须对MVC 5做任何事情。

If you are upgrading MVC4 app to MVC5 follow: 如果您要将MVC4应用程序升级到MVC5,请执行以下操作:

http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2 http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and- Web的API-2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM