mvc中seeeion和cook的用法
public ActionResult A()
{ Session["test"]="123"; return View();}public ActionResult B(){ string v=Session["test"].ToString(); return View();}这样创建session是获取不到的,原因是因为创建的session是Controller下的(这里自己可以深入研究一下),而不是System.Web.HttpContext.Current的session。
想要所有的地方都可以获取session的值,应该是如下写法
public ActionResult A()
{ System.Web.HttpContext.Current.Session["test"]="123"; return View();}public ActionResult B(){ string v=System.Web.HttpContext.Current.Session["test"].ToString(); return View();}HttpCookie Cookie = null;
Cookie = new HttpCookie("User");
if (Cookie != null)
{
Cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Set(Cookie);
}
HttpCookie cookie = new HttpCookie("MyCook");
DateTime dt = DateTime.Now;
cookie.Expires = dt.AddMinutes(5); //五分钟过期
cookie.Values.Add("userid", "dsc");
cookie.Values.Add("username", HttpUtility.UrlEncode("段世昌")); //注意汉子存储的时候,如果直接存的是汉字,读取的时候值是乱码,因此要先把汉子编码,读取的时候再解码就ok了,字母就不用编码了
Response.AppendCookie(cookie);
-读
if (Request.Cookies["MyCook"] != null)
{
// HttpCookie cookie=Request.Cookies["MyCook"];
//string s=cookie["userid"].ToString();//整行
string c= Request.Cookies["MyCook"]["userid"].ToString();//整行
string c3= HttpUtility.UrlDecode(Request.Cookies["MyCook"]["username"]); //汉字要解码的
}
mvc中seeeion和cook的用法的更多相关文章
- Spring mvc中@RequestMapping 6个基本用法
Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: Java代码 @Reques ...
- Spring mvc中@RequestMapping 6个基本用法小结
Spring mvc中@RequestMapping 6个基本用法小结 小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMa ...
- 转:Spring mvc中@RequestMapping 6个基本用法小结
Spring mvc中@RequestMapping 6个基本用法小结 发表于3年前(2013-02-17 19:58) 阅读(11698) | 评论(1) 13人收藏此文章, 我要收藏 赞3 4 ...
- Spring mvc中@RequestMapping 6个基本用法小结(转载)
小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...
- 第九节:从源码的角度分析MVC中的一些特性及其用法
一. 前世今生 乍眼一看,该标题写的有点煽情,最近也是在不断反思,怎么能把博客写好,让人能读下去,通俗易懂,深入浅出. 接下来几个章节都是围绕框架本身提供特性展开,有MVC程序集提供的,也有其它程序集 ...
- Spring MVC中@RequestParam/@RequestBody/@RequestHeader的用法收集(转)
简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri部分(这里指uri template中 ...
- Spring MVC中的ModelMap作用及用法
ModelMap的作用: ModelMap对象主要用于传递控制方法传递数据到结果页面.类似于request的setAttribute方法的作用. 所以我们要想在jsp页面获取数据,只要将数据放到Mod ...
- Spring mvc中@RequestMapping 6个基本用法整理
继续整理,这个是前段时间用jsp开发的一个站点,说起来php程序员去做jsp程序确实有些小不适应,但是弄完后绝对对于这种强类型语言而比收获还是颇多的. 1,最基本的,方法级别上应用 @RequestM ...
- MVC 中的@Html.Raw 的用法
@Html.Raw 定义:在有些情况下,需要显式地渲染一些不应该采用HTML编码的值,这时可以采用Html.Raw方法来保证该值不被编码:简单来说:就是使用了Html.Raw后,字符串会以一个html ...
随机推荐
- C# Socket 实现WebSocket服务器端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- NetCore偶尔有用篇:NetCore项目发布为Nuget包
一.简介 1.nuget大家已经不陌生. 2.netcore默认引用便是nuget,并处理了嵌套关系. 3.netcore已经支持直接编译生成nuget包. 4.本文介绍如何把自己建立的项目发布为nu ...
- Linq to sql中使用DateDiff()
Linq to sql中使用DateDiff() 计算时间差的方法 第一种办法: from p in PurchaseLists where EntityFunctions.DiffDays(p.Cr ...
- java模板
public class max { public static void main(String[]args){ """ /* xxx */ ""& ...
- session 和cookie
(1)cookie与session---------->>>>>>>>>>>>>>>>>>& ...
- 【UVA11107 训练指南】Life Forms【后缀数组】
题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...
- hot code loading in nodejs
Hot Code Loading in Node.js Node.js Web应用代码热更新的另类思路 Reading through Fever today, this post by Jack M ...
- 80端口被系统进程PID-4占用解决办法
今天因为工程需要就把tomcat服务器的端口改成了80了,可是一启动就出现问题了 发现报错信息是端口占用了,于是我马上就在了命令行敲入了netstat -ano查看端口占用情况 终于发现是PID为4的 ...
- Educational Codeforces Round 25 C. Multi-judge Solving
题目链接:http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test 1 second ...
- 【BZOJ3143】【HNOI2013】游走 高斯消元
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3143 我们令$P_i$表示从第i号点出发的期望次数.则$P_n$显然为$0$. 对于$P ...