zoukankan      html  css  js  c++  java
  • Session的使用

    我想在ashx.cs中或者在class.cs中使用session,但是发现总是出现

    “未将引用设置到实例的错误”

    最后发现IHttpHandler,IRequiresSessionState 并不能对session进行修改

    最后加了这些才可以,实现了对session的读取与修改。

    最好把session和cookie方法封装成类

    CookieHelper:

    /*
     源码己托管:http://git.oschina.net/kuiyu/dotnetcodes
     */
    
    using System;
    using System.Web;
    
    namespace DotNet.Utilities
    {
        public class CookieHelper
        {
            /// <summary>
            /// 清除指定Cookie
            /// </summary>
            /// <param name="cookiename">cookiename</param>
            public static void ClearCookie(string cookiename)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddYears(-3);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
            }
            /// <summary>
            /// 获取指定Cookie值
            /// </summary>
            /// <param name="cookiename">cookiename</param>
            /// <returns></returns>
            public static string GetCookieValue(string cookiename)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
                string str = string.Empty;
                if (cookie != null)
                {
                    str =HttpUtility.UrlDecode(cookie.Value);
                }
                return str;
            }
            /// <summary>
            /// 添加一个Cookie(24小时过期)
            /// </summary>
            /// <param name="cookiename"></param>
            /// <param name="cookievalue"></param>
            public static void SetCookie(string cookiename, string cookievalue)
            {
                SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(1.0));
            }
            /// <summary>
            /// 添加一个Cookie
            /// </summary>
            /// <param name="cookiename">cookie名</param>
            /// <param name="cookievalue">cookie值</param>
            /// <param name="expires">过期时间 DateTime</param>
            public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
            {
                HttpCookie cookie = new HttpCookie(cookiename)
                {
                    Value = cookievalue,
                    Expires = expires
                };
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }

    SessionHelper:

    /*
     源码己托管:http://git.oschina.net/kuiyu/dotnetcodes
     */
    using System.Web;
    
    namespace DotNet.Utilities
    {
        public static class SessionHelper2
        {
            /// <summary>
            /// 添加Session,调动有效期为20分钟
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            /// <param name="strValue">Session值</param>
            public static void Add(string strSessionName, string strValue)
            {
                HttpContext.Current.Session[strSessionName] = strValue;
                HttpContext.Current.Session.Timeout = 20;
            }
    
            /// <summary>
            /// 添加Session,调动有效期为20分钟
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            /// <param name="strValues">Session值数组</param>
            public static void Adds(string strSessionName, string[] strValues)
            {
                HttpContext.Current.Session[strSessionName] = strValues;
                HttpContext.Current.Session.Timeout = 20;
            }
    
            /// <summary>
            /// 添加Session
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            /// <param name="strValue">Session值</param>
            /// <param name="iExpires">调动有效期(分钟)</param>
            public static void Add(string strSessionName, string strValue, int iExpires)
            {
                HttpContext.Current.Session[strSessionName] = strValue;
                HttpContext.Current.Session.Timeout = iExpires;
            }
    
            /// <summary>
            /// 添加Session
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            /// <param name="strValues">Session值数组</param>
            /// <param name="iExpires">调动有效期(分钟)</param>
            public static void Adds(string strSessionName, string[] strValues, int iExpires)
            {
                HttpContext.Current.Session[strSessionName] = strValues;
                HttpContext.Current.Session.Timeout = iExpires;
            }
    
            /// <summary>
            /// 读取某个Session对象值
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            /// <returns>Session对象值</returns>
            public static string Get(string strSessionName)
            {
                if (HttpContext.Current.Session[strSessionName] == null)
                {
                    return null;
                }
                else
                {
                    return HttpContext.Current.Session[strSessionName].ToString();
                }
            }
    
            /// <summary>
            /// 读取某个Session对象值数组
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            /// <returns>Session对象值数组</returns>
            public static string[] Gets(string strSessionName)
            {
                if (HttpContext.Current.Session[strSessionName] == null)
                {
                    return null;
                }
                else
                {
                    return (string[])HttpContext.Current.Session[strSessionName];
                }
            }
    
            /// <summary>
            /// 删除某个Session对象
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            public static void Del(string strSessionName)
            {
                HttpContext.Current.Session[strSessionName] = null;
            }
        }
    }
    View Code
  • 相关阅读:
    分布式与集群的区别是什么?
    Java NIO:IO与NIO的区别 JAVA BIO与NIO、AIO的区别
    localStorage使用总结 JS 详解 Cookie、 LocalStorage 与 SessionStorage
    tomcat+nginx+redis实现均衡负载、session共享 存储过程的优缺点 HTTP、TCP、IP协议常见面试题
    高并发下的Java数据结构(List、Set、Map)
    [剑指offer] 31. 整数中1出现的次数(从1到n整数中1出现的次数)
    [剑指offer] 30. 连续子数组的最大和
    [剑指offer] 29. 最小的K个数
    [剑指offer] 28. 数组中出现次数超过一半的数字
    [leetcode] 51. N-Queens (递归)
  • 原文地址:https://www.cnblogs.com/ithuo/p/4882831.html
Copyright © 2011-2022 走看看