日期:2014-05-17  浏览次数:20518 次

MVC Session怎么回事?要设置两次SESSION才可以取得到值,为什么呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVCSession.Controllers
{
    public class TestController : Controller
    {
        public ActionResult Index()
        {
            string sessionData = "Now "+DateTime.Now.ToString();
            HttpContext.Session["Data"] = sessionData;
            return Content(string.Format("SessionID={0}\nSession={1}\n", HttpContext.Session.SessionID, sessionData), "text/txt");
        }
 
        public ActionResult GetSession()
        {
            string sessionData = (string)HttpContext.Session["Data"];
            return Content(string.Format("SessionID={0}\nSession={1}\n", HttpContext.Session.SessionID,sessionData== null ? "Null" : sessionData, "text/txt"));
        }
 
    }
}


就是新建立一个默认的MVC4,这种问题是怎么回事?还是我用法不正确?

设置
http://host/Test/Index SessionID=oz25ujr2mbtf0g1p3q0c200c Session=Now 2014-2-1 12:53:51 

获取
http://host/Test/GetSession SessionID=oz25ujr2mbtf0g1p3q0c200c Session=Null 

设置那里运行第一次是取不得的。运行第二次后,获取才可以取得。是怎么回事?

------解决方案--------------------
一次应该就可以,不过确认下浏览器没有缓存页面而导致并没有真正请求服务器。
------解决方案--------------------
载入的文件先后有问题吧