MVC4会话不请求之间仍然存在 [英] MVC4 Session not persisted between requests

查看:122
本文介绍了MVC4会话不请求之间仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大更新后,一些很好的测试

我有一个MVC4 Web应用程序,我只是调试它在Visual Studio 2010只是学习一些更多的web开发(和MVC尤其是)。我现在玩会话。但我不明白为什么我一个新的htt prequest后失去我的变量。

I have a MVC4 web application and I'm simply debugging it in visual studio 2010 just to learn some more about webdevelopment (and MVC in particular). I'm playing with Session now. But I don't understand why I lose my variables after a new httprequest.

是这样的问题:失去我的会话变量

我对会议的部分web.config中如下:

My web.config for the session part looks like:

<sessionState mode="InProc"
   cookieless="false"
   timeout="20"/>

我的小测试项目,以找出问题工作正常,看起来像:

My little test project to isolate the problem works fine and looks like:

控制器
- HomeController的

Controllers - HomeController

public class HomeController : Controller
{
    //
    // GET: /Test/

    public ActionResult Index()
    {
        string t = (string)Session["Test1"];
        ViewBag.Result = t;
        return View();
    }

}


  • MysessionController

    • MysessionController

      公共类MysessionController:控制器
          {
              //
              // GET:/ MySession的/

      public class MysessionController : Controller { // // GET: /Mysession/

          public ActionResult Index()
          {
              return View(new Models.Mysession() {ID = Session.SessionID});
          }
      
          [HttpPost]
          public ActionResult Index(Models.Mysession mySession) {
              Session["Test1"] = "Bla";
              return RedirectToAction("Index", "Home");
          }
      
      }
      


    • 模式

      public class Mysession {
          [Required]
          public string ID { get; set; }
      }
      

      浏览


      • 首页

      • Home


      • Index.cshtml

      • Index.cshtml

      @ {
         ViewBag.Title =指数;
      }

      @{ ViewBag.Title = "Index"; }

      @ @ ViewBag.Result 的@
      @((字符串)会议[Test1的])
      @ Html.ActionLink(我的会议,索引,MySession的)

      @@ViewBag.Result@ @((string)Session["Test1"]) @Html.ActionLink("My session", "Index", "Mysession")

      MySession的

      Mysession


      • Index.cshtml

      • Index.cshtml

      @model SessionTest.Models.Mysession
      @ {
      ViewBag.Title =指数;
      }

      @model SessionTest.Models.Mysession @{ ViewBag.Title = "Index"; }

      @using(Html.BeginForm()){

      @using (Html.BeginForm()) {

      @Html.ValidationSummary(false)
      <fieldset>
          <legend>Mysession</legend>
           <div class="editor-label">
              @Html.LabelFor(model => model.ID)
          </div>
          <div class="editor-field">
              @Html.EditorFor(model => model.ID)
          </div>
          <p>
              <input type="submit" value="Log in" />
          </p>
      </fieldset>
      

      }

      这看起来像我的主要应用简单地说,所不同的是:在在会话变量设置存取权限我一个XML文件来查找东西HTTPPOST Index操作MysessionController。

      This looks in a nutshell like my main application, the DIFFERENCE is: In MysessionController in the HTTPPOST Index action where the session variable is set I acces a XML file to look up something.

      XML文件是我的持久化存储,因为我没有一个SQL服务器。我的问题是,这会影响我的会议?

      The XML file is my persistent storage because I don't have a SQL server. My question is, can this affect my session?

      如果是这样,我想指出,我的存取权限的XML文件之前,在会话设置变量。因此,它似乎很奇怪我。但我已经通过测试,如果我没有存取权限的XML文件,然后在会话的增值经销商都很好精确定位。如果我的存取权限XML文件中的会话瓦尔的RedirectToAction后是空的。

      If so, I would like to point out that I acces the XML file PRIOR to setting the variable in the session. So it seems strange to me. But I've pinpointed by testing that if I don't acces the XML file then the vars in the session are fine. If I acces the XML file the vars in the session are null after the RedirectToAction.

      我不知道为什么我昨天没有来到这个结论。对不起大家。我昨天做了一些马虎的测试。

      I don't know why I didn't came to this conclusion yesterday. Sorry everybody. I did some sloppy testing yesterday.

      推荐答案

      首先,我想感谢大家,已经把时间这个问题。之后我得到了自己在一起,最后进行了适当的测试我明确指出这个问题到硬盘驱动器上访问XML文件。不知道是什么我搞乱昨天...

      First off, I would like to thank everybody that has put time in this question. After I got myself together and finally conducted a proper test I pinpointed the problem to accessing a XML file on the hard drive. Don't know what I was messing about yesterday...

      我发现谷歌低谷几个帖子和文章有关访问文件后,失去您的会话。像这样的:

      I found trough Google several posts and articles about losing your session after accessing files. Like this one:

      <一个href=\"http://blogs.msdn.com/b/tess/archive/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles.aspx\">http://blogs.msdn.com/b/tess/archive/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles.aspx

      由泰莎Ferrandez神奇的案例研究。而对于较小的扩展:
      http://forums.asp.net/t/998370.aspx/1

      Fantastic case study by Tessa Ferrandez. And to lesser extend: http://forums.asp.net/t/998370.aspx/1

      <一个href=\"http://blogs.msdn.com/b/toddca/archive/2005/12/01/499144.aspx\">http://blogs.msdn.com/b/toddca/archive/2005/12/01/499144.aspx

      结论

      当您在您的网站的任何方式访问文件的应用程序域将被刷新,这将刷新您的会话。

      When you access files in any way of your website the AppDomain will be refreshed and this will refresh your session.

      不要把你需要通过你的网站在你的'bin文件夹访问文件。我搬到我的XML文件的App_Data和变量在会话持久。

      Never put a file you need to access through your website in your 'bin' folder. I've moved my XML file to App_Data and the variables are persisted in the Session.

      总结了我的答案在我的博客:<一href=\"http://dannyvanderkraan.word$p$pss.com/2013/08/21/losing-session-variables-with-asp-net/\">http://dannyvanderkraan.word$p$pss.com/2013/08/21/losing-session-variables-with-asp-net/

      Summed up my answer on my blog: http://dannyvanderkraan.wordpress.com/2013/08/21/losing-session-variables-with-asp-net/

      这篇关于MVC4会话不请求之间仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆