C#Mvc中Session、Cookie、Application、Cache的简单传值取值

//声明一个变量
			string str = ""


            //给session传值
            Session["str"] = "值";
            //session取值
            str= Session["str"].ToString();


            //给cookie传值
            HttpCookie cookie1 = new HttpCookie("Name");
            cookie1["str"] = "值";
            //将此代码发送到浏览器,浏览器会自动生成一个新的cookie,往Cookie里面添加一个新的cookie
            Response.Cookies.Add(cookie1);

            
            //cookie取值
            HttpCookie cookie2 = System.Web.HttpContext.Current.Request.Cookies["Name"];
            if (cookie2 != null)
            {
                if (cookie2["str"] != null)
                {
                    str= System.Web.HttpContext.Current.Server.UrlDecode(cookie2["str"]);
                }
            }


            //Application传值
            HttpContext.Application["str"] = "值";

            //Application取值
            str = HttpContext.Application["str"].ToString();


            //cache传值
            HttpContext.Cache["str"] = "值";

            //cache取值
            str = HttpContext.Cache["str"].ToString();


发布了43 篇原创文章 · 获赞 35 · 访问量 1573

猜你喜欢

转载自blog.csdn.net/qq_45244974/article/details/103926747