Mvc中:

session:

if (!string .IsNullOrEmpty(find))   //设置
Session["oip"] = "无锡";
ViewBag.oip =Session["oip"]; if (Session["oip"] == null) //获取
Session["oip"] = null; //设为null Session.Timeout = 1;          //设置过期时间 <sessionState mode="InProc" timeout="30"/> //过期30分钟
或者:
存:

Session.Timeout = 960;
Session["customerUser"] = (from c in db.C_CustomerUser where c.Mobile == mobile select c).FirstOrDefault();

取:

private HttpSessionState session = HttpContext.Current.Session;

public C_CustomerUser GetCustomers()
{
if (session["customerUser"] != null)
{
C_CustomerUser customer = session["customerUser"] as
C_CustomerUser;
return customer;
}


cookie:

HttpCookie cookie = new HttpCookie("oip");
cookie.Expires = System.DateTime.Now.AddYears();
cookie["oipp"] = "用户名";
Response.Cookies.Add(cookie); //设置
<sessionState mode="InProc" timeout=""/> //设置过期时间
cookie["oipp"] =null;
HttpCookie cookiee = Response.Cookies.Get("oip"); //获取
ViewBag.oip = cookiee["oipp"]; 或者
HttpCookie cookie = new HttpCookie("City"); //初使化并设置Cookie的名称   //设置
TimeSpan ts = new TimeSpan(, , , , ); //过期时间为 1天
cookie.Expires = dt.Add(ts); //设置过期时间
cookie.Values.Add("CityID", strCityID);
cookie.Values.Add("CityOrg", strCityOrg);
cookie.Values.Add("CityName", strCityName);
HttpContext.Current.Response.AppendCookie(cookie); 取: HttpCookie cityCookie = System.Web.HttpContext.Current.Request.Cookies["City"];
if (cityCookie != null)
{
city = cityCookie["CityID"]; }

控件中:

 Session["UserInfo"]   //设置和获取
参考:http://www.cnblogs.com/kevin-top/archive/2010/07/04/1770726.html
cookie: C# 计算时间差 用timespan函数http://www.blogjava.net/AndyZhang/archive/2012/05/02/377157.html
 HttpCookie cookie=new HttpCookie("MyCook");//初使化并设置Cookie的名称      //设置
 DateTime dt=DateTime.Now;
 TimeSpan ts = new TimeSpan(, , ,,);//过期时间为1分钟
 cookie.Expires = dt.Add(ts);//设置过期时间
 cookie.Values.Add("userid", "userid_value");
 cookie.Values.Add("userid2","userid2_value2");
 Response.AppendCookie(cookie);

设置

if(Request.Cookies["MyCook"]!=null)                        //获取
 {
   //Response.Write("Cookie中键值为userid的值:" + Request.Cookies["MyCook"]["userid"]);//整行
   //Response.Write("Cookie中键值为userid2的值" + Request.Cookies["MyCook"]["userid2"]);
   Response.Write(Request.Cookies["MyCook"].Value);//输出全部的值
 }

获取

//获取客户端的Cookie对象
HttpCookie cok = Request.Cookies["MyCook"];               //修改 新增
 if (cok != null)
{
    //修改Cookie的两种方法
    cok.Values["userid"] = "alter-value";
    cok.Values.Set("userid", "alter-value");     //往Cookie里加入新的内容
    cok.Values.Set("newid", "newValue");
    Response.AppendCookie(cok);
}

修改、新增

      HttpCookie cok = Request.Cookies["MyCook"];
    if (cok != null)
    {
      if (!CheckBox1.Checked)
      {
        cok.Values.Remove("userid");//移除键值为userid的值
      }
      else
      {
        TimeSpan ts = new TimeSpan(-, , , );
        cok.Expires = DateTime.Now.Add(ts);//删除整个Cookie,只要把过期时间设置为现在
      }
      Response.AppendCookie(cok);
    }

删除

 

一般处理程序:

session:

context.Session["ws_user"].ToString()

Session["UserInfo"] = currentUser;

cookie:

HttpCookie cookie = HttpContext.Current.Request.Cookies["info"];
// cookie = null;
if (cookie == null )
{
cookie = new HttpCookie("Info");                              //设置
cookie["CityID"] = HttpContext.Current.Server.UrlEncode(cityID); //编码
cookie["CityName"] = HttpContext.Current.Server.UrlEncode(CityName);
cookie.Expires = DateTime.Now.AddDays();//
HttpContext.Current.Response.Cookies.Add(cookie);
}else{
//直接读值,注意编码 解码、不然汉字会出现乱码。
Server.UrlDecode()
}
# 用来标志特定的文档位置 %
% 对特殊字符进行编码 %
& 分隔不同的变量值对 %
+ 在变量值中表示空格 %2B
\ 表示目录路径 %2F
= 用来连接键和值 %3D
? 表示查询字符串的开始 %3F
       HttpCookie cookie = new HttpCookie("Test");//初使化并设置Cookie的名称

        TimeSpan ts = new TimeSpan(, , , , );//过期时间为1分钟
cookie.Expires = DateTime.Now.Add(ts);//设置过期时间
cookie.Values.Add("userid", "");
cookie.Values.Add("test", "THIS_IS_TEST");
context.Response.AppendCookie(cookie); context.Response.Write(context.Request.Cookies["Test"].Value); //获取

MVC、控件、一般处理程序中的session and cookie的更多相关文章

  1. 9.2.2 .net framework下的MVC 控件的封装(下)

    控件封装的部分说明 可能有人觉得应该前后端分离,我也承认这是应该的方向,我们也在考虑使用ng2等简化前端.但是,我们封装控件还是因为如下原因综合考虑的: 我们这是个框架,上面支撑了许多个应用,包含几百 ...

  2. 9.2.1 .net framework下的MVC 控件的封装(上)

    在写.net core下mvc控件的编写之前,我先说一下.net framework下我们MVC控件的做法. MVC下控件的写法,主要有如下三种,最后一种是泛型的写法,mvc提供的控件都是基本控件. ...

  3. 如何自定义MVC控件?

    今天公司要写学习总结,想着想着还是先写一篇关于MVC内部什么东东的博客整理整理再发表吧,一举两得. 之前写过了路由.过滤器等.今天就研究一下怎么自定义MVC控件吧. 本人技术小菜,不喜勿喷.....( ...

  4. GridView控件RowDataBound事件中获取列字段值的几种途径

    前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...

  5. FineUIMvc v1.4.0 发布了(ASP.NET MVC控件库)!

    FineUIMvc v1.4.0 已经于 2017-06-30 发布,FineUIMvc 是基于 jQuery 的专业 ASP.NET MVC 控件库,是我们的新产品.由于和 FineUI(专业版)共 ...

  6. 【转载】OLE控件在Direct3D中的渲染方法

    原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么 ...

  7. ASP.NET中在一般处理程序中使用session的简单介绍

    这篇文章介绍了ASP.NET中在一般处理程序中使用session,有需要的朋友可以参考一下 <%@ WebHandler Language="C#" Class=" ...

  8. 基于 jQuery 的专业 ASP.NET WebForms/MVC 控件库!

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  9. My97DatePicker时间控件在项目中的应用

    一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...

随机推荐

  1. 实验1 OpenGL初识

    实验预备知识 Windows下的OpenGL编程步骤简单介绍详见课程实验教学博客-实验准备安装GLUT包与创建工程: http://www.cnblogs.com/opengl/archive/201 ...

  2. 杭电2061WA

    #include<stdio.h> struct mem { char s[50]; int c; int f; }; int main() { struct mem x[60]; int ...

  3. win系统如何在桌面显示我的电脑

    如果是在Windows Server 2012本地控制台下,直接按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dll,Control_RunDLL desk.c ...

  4. vs code格式化代码快捷键

    windows:shift+alt+F ubuntu: ctrl+shift+i

  5. Nginx+keepalived双机热备(默认路径安装)- 基础篇

    负载均衡技术对于一个网站尤其是大型网站的web服务器集群来说是至关重要的!做好负载均衡架构,可以实现故障转移和高可用环境,避免单点故障,保证网站健康持续运行.关于负载均衡介绍,可以参考:linux负载 ...

  6. Javascript中的null和 undefined

    Javascript Undefined vs NULL Many a times we often get confused on whats the difference between UNDE ...

  7. 移动端使用rem时候动态设置html字体大小

      !(function(doc, win) { var docEle = doc.documentElement, evt = "onorientationchange" in ...

  8. [noip2011 luogu1312] Mayan游戏(模拟)

    原题:传送门 大模拟- 两个剪枝: 1.如果左边不为空就不往左边走(因为一定不如左边的移到右边优) 2.如果相邻两颜色相同不需移动 当然也有别的小剪枝(我没写)比如如果当前某一颜色剩余块数满足1< ...

  9. P3369 【模板】普通平衡树 (splay 模板)

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...

  10. MySQL 数据库类型