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. ios 编译版本 最低版本 运行版本 动态链接库

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) 运行环境判断: #if __IPHONE_OS_VERSION_ ...

  2. MVC 接收文件

    [HttpPost] public ActionResult Layedit() { var files = Request.Files; //获得所上传的所有文件 ) { HttpPostedFil ...

  3. 学了5天Arm,今天谈谈初学感受 (转)

    一.初探      4月1日入手友善mini2440.先看了下板子,感觉没什么稀奇的,s3c2440总线上外挂SDRAM(对这个不是很感冒,之前搞过一个FPGA的SDRAM控制器),NOR    .  ...

  4. 网站顶部显示预加载进度条preload.js

    网站加载的速度快的话,不会显示进度条加载时候的样式. 支持性主流浏览器都支持,ie浏览器需要9以上9也支持. 使用方法 <script src="http://code.jquery. ...

  5. JS 100节楼梯,0-49节 分数等于节数 50以后(包括50)每节10分输入节数 得出分数

    var n = parseInt(prompt("请输入数值")); ; ; ){ ; i<n; i++) { sum = sum + i; } alert(sum); } ...

  6. 深入理解B/S与C/S架构

    首先来介绍一下B/S与C/S架构 C/S架构简要介绍 在了解什么是B/S架构之前,我们有必要了解一下什么是C/S架构: C/S架构是第一种比较早的软件架构,主要用于局域网内.也叫 客户机/服务器模式. ...

  7. 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...

  8. SSM知识巩固

    ------------------------- 绑定页面提交的多个数据  绑定数组 --------------------------------------- 绑定list(需求:批量修改商品 ...

  9. CodeForcesGym 100641D Generalized Roman Numerals

    Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on  ...

  10. BA--空调系统一次泵和二次泵区别

    通常来说,空调系统是按照满负荷设计的,但实际运行中,满负荷运行的 时间不足 3% ,空调设备绝大部分时间内在远低于额定负荷的情况下运转.在 部分负荷下,虽然冷水机组可以根据实际负荷调节相应的冷量输出, ...