MVC、控件、一般处理程序中的session and cookie
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的更多相关文章
- 9.2.2 .net framework下的MVC 控件的封装(下)
控件封装的部分说明 可能有人觉得应该前后端分离,我也承认这是应该的方向,我们也在考虑使用ng2等简化前端.但是,我们封装控件还是因为如下原因综合考虑的: 我们这是个框架,上面支撑了许多个应用,包含几百 ...
- 9.2.1 .net framework下的MVC 控件的封装(上)
在写.net core下mvc控件的编写之前,我先说一下.net framework下我们MVC控件的做法. MVC下控件的写法,主要有如下三种,最后一种是泛型的写法,mvc提供的控件都是基本控件. ...
- 如何自定义MVC控件?
今天公司要写学习总结,想着想着还是先写一篇关于MVC内部什么东东的博客整理整理再发表吧,一举两得. 之前写过了路由.过滤器等.今天就研究一下怎么自定义MVC控件吧. 本人技术小菜,不喜勿喷.....( ...
- GridView控件RowDataBound事件中获取列字段值的几种途径
前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...
- FineUIMvc v1.4.0 发布了(ASP.NET MVC控件库)!
FineUIMvc v1.4.0 已经于 2017-06-30 发布,FineUIMvc 是基于 jQuery 的专业 ASP.NET MVC 控件库,是我们的新产品.由于和 FineUI(专业版)共 ...
- 【转载】OLE控件在Direct3D中的渲染方法
原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么 ...
- ASP.NET中在一般处理程序中使用session的简单介绍
这篇文章介绍了ASP.NET中在一般处理程序中使用session,有需要的朋友可以参考一下 <%@ WebHandler Language="C#" Class=" ...
- 基于 jQuery 的专业 ASP.NET WebForms/MVC 控件库!
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- My97DatePicker时间控件在项目中的应用
一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...
随机推荐
- Vue报错笔记
1.错误信息:[Vue warn]: Property or method "object" is not defined on the instance but referenc ...
- JQuery学习笔记系列(一)----选择器详解
笔者好长时间没有更新过博客园的笔记了,一部分原因是去年刚刚开始工作一段时间忙碌的加班,体会了一种每天加班到凌晨的充实感,之后闲暇时间了也因为自己懒惰没有坚持记笔记的习惯,现在重新拾起来. 借用古人的一 ...
- WP - 控件基础-按钮控件
Button:HyperlinkButton:RepeatButton:ToggleButton 1.Button: <button content="Button" ...
- Spring AOP 介绍与基于接口的实现
热烈推荐:超多IT资源,尽在798资源网 声明:转载文章,为防止丢失所以做此备份. 本文来自公众号:程序之心 原文地址:https://mp.weixin.qq.com/s/vo94gVyTss0LY ...
- java中Map遍历的四种方式
在java中所有的map都实现了Map接口,因此所有的Map(如HashMap, TreeMap, LinkedHashMap, Hashtable等)都可以用以下的方式去遍历. 方法一:在for循环 ...
- TensorFlow+实战Google深度学习框架学习笔记(12)------Mnist识别和卷积神经网络LeNet
一.卷积神经网络的简述 卷积神经网络将一个图像变窄变长.原本[长和宽较大,高较小]变成[长和宽较小,高增加] 卷积过程需要用到卷积核[二维的滑动窗口][过滤器],每个卷积核由n*m(长*宽)个小格组成 ...
- Python 不同列表时间测试
import timeit import threading def test1(): l = [] for i in range(1000): l = l + [i] def test2(): l ...
- GOF23设计模式之建造者模式
GOF23设计模式之建造者模式 场景: 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样的一个问题需要处理: 装配这些子组件是不是有个步骤问题? 实际开发中,我们所 ...
- anaconda jupyter
本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...
- github插件
可能是迄今为止最好的GitHub代码浏览插件,基本实现浏览器变成代码阅读器,支持目录列表,交叉索引等功能: O网页链接 http://weibo.com/1963193953/Fdj2cFQ ...