请问在asp.net 中一般处理程序 怎么获取 cookie?

asp.net 一般处理程序 怎么获取 cookie


慕仙森
浏览 286回答 5
5回答

千万里不及你

不都一样么?使用方法都是一样的。没有差别。1234567891011121314public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "text/plain";        context.Response.Write("Hello World");         HttpCookie cookie = new HttpCookie("Test");//初使化并设置Cookie的名称                 TimeSpan ts = new TimeSpan(0, 0, 1, 0, 0);//过期时间为1分钟        cookie.Expires = DateTime.Now.Add(ts);//设置过期时间        cookie.Values.Add("userid", "123456");        cookie.Values.Add("test", "THIS_IS_TEST");        context.Response.AppendCookie(cookie);         context.Response.Write(context.Request.Cookies["Test"].Value);    }

MM们

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(10);//HttpContext.Current.Response.Cookies.Add(cookie);}else{//直接读值,注意编码 解码、不然汉字会出现乱码。}

哆啦的时光机

.net的一般处理程序 .ashx的context对象默认是取不出session的值出来的。要达到取出Session的效果,则需要让它实现System.Web.SessionState.IReadOnlySessionState接口(该接口没有任何方法实现,只是起到一个标识作用)为了让所有的一般处理程序都能获取到Session值,并且能集中做一些控制管理(比如用户认证、权限控制等),我的策略是让一个抽象类实现IHttpHandler, IRequiresSessionState接口,然后让其他所有一般处理程序都继承该抽象类即可。
打开App,查看更多内容
随时随地看视频慕课网APP