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最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...
随机推荐
- swift 类型备份
Swift语法3.03(类型Types) https://www.jianshu.com/p/839f9bc4b9a3 https://developer.apple.com/library/cont ...
- Auto Layout压缩阻力及内容吸附讲解
Auto Layout压缩阻力及内容吸附讲解 本文为投稿文章,作者:梁炜V 在Auto Layout的使用中,有两个很重要的布局概念:Content Compression Resistance 和 ...
- Java中Scanner类的使用
一个可以解析基本类型和字符串的简单文本扫描器. 例如,以下代码使用户能够从 System.in 中读取一个数: public class ApiScanner { public static void ...
- Apex语言(一)开发环境
1.注册salesforce开发者https://developer.salesforce.com/ 2.开发者登录https://login.salesforce.com/ 3.Apex开发者工具 ...
- AppScan用来进行漏洞扫描
https://www.cnblogs.com/mawenqiangios/p/8573525.html
- Angular之constructor和ngOnInit差异及适用场景(转)
原始地址:https://blog.csdn.net/u010730126/article/details/64486997 Angular中根据适用场景定义了很多生命周期函数,其本质上是事件的响应函 ...
- 浅谈urllib和requests
urllib和requests的学习 urllib requests 参考资料 urllib urllib是python的基本库之一,内置四大模块,即request,error,parse,robot ...
- 《黑白团团队》第八次团队作业:Alpha冲刺 第四天
项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 团队项目Github仓库地址链接. 第四天 日期:2019/6/18 成员 ...
- IAR for MSP430 关于添加自定义头文件的两种方法【转】
前言:第一次接触这个软件,编译一个例程一直出现没有包含头文件的错误,在网上找了好几个方法都没什么效果,看到了篇文章,利用里面的方法1解决了,特此复制下来保存学习用. 原文链接:https://blog ...
- cliendataset中自增长字段的处理
cliendataset中自增长字段的处理: id:自增长字段. 在client中的处理方法:clientdataset.Fields.FieldByName('id').ReadOnly:=Fals ...