利用cookie传值

时间:2025-02-09 16:48:15

   前台方法

 function queryListByMonth() {
            var queryMonth = $('#QueryTime').val();
            $('#viewChapterID').val(queryMonth);
             = '../EmployeeServices/PunchList?queryMonth=' + ();
        };

   后台写入cookie

public ActionResult PunchList()
        {
            var strMonth = Request["queryMonth"];
            if (strMonth != null)
            {
                var cookiesMonth = new HttpCookie("queryMonth");
                cookiesMonth["monthValue"] = strMonth;
                 = (1);
                (cookiesMonth);
            }
            var model = new EmployeeServicePunchList();
             = QueryPurchList();
            return View(model);
        }

   后台读取cookie 

HttpCookie cookie = ["queryMonth"];
            if (cookie != null)
            {
                var month = cookie["monthValue"];
                TimeSpan ts = new TimeSpan(0, 0, 0, 0);//时间跨度 
                ("cookie");//清除 
                startTime = (month);
                endTime = LastDayOfMonth(startTime);
            }

   如果在自己在编写的过程中,不想让别人在浏览器中查看cookie的值,我们可以给cookie加密,取值的时候再解密。代码如下

cookiesMonth["monthValue"]  = (queryMonth, ("UTF-8"));//加密
      HttpCookie demo= ["(queryMonth"]
      (demo["monthValue"]);//解密

   cookie传值,耗费性能,大家慎用,但是在某些场合还是可以考虑的,其实cookie还有很多用途,期待大家的解密。