• ASP.NET的session操作方法总结


       在开发ASP.NET程序时,需要对相关数据进行缓存,缓存较多的主要是用户的身份信息,现提供几个对session操作较为常用的方法:

    1.添加session,对设置对应的时间:

            /// <summary>
            /// 添加Session,调动有效期为30分钟
            /// </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 = 30;
            }
            /// <summary>
            /// 添加Session,调动有效期为30分钟
            /// </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 = 30;
            }
            /// <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;
            }

       以上对session的添加操作中,主要是参数值的类型不同,以后后两种方式中,对session的有效时间进行滑动设置。

    2.session的读取操作:

            /// <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;
                }
                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;
                }
                return (string[])HttpContext.Current.Session[strSessionName];
            }

    3.session的删除操作:

            /// <summary>
            /// 删除某个Session对象
            /// </summary>
            /// <param name="strSessionName">Session对象名称</param>
            public static void Del(string strSessionName)
            {
                HttpContext.Current.Session[strSessionName] = null;
            }

        session的操作中主要包含ID和Value,无论是写入还是读取,SessionId可以做唯一性标记。

    4.HttpContext和HttpSessionState的源码如下:

      /// <summary>
      /// 封装有关个别 HTTP 请求的所有 HTTP 特定的信息。
      /// </summary>
      public sealed class HttpContext : IServiceProvider, IPrincipalContainer
      {
        /// <summary>
        /// 使用指定的请求和响应对象初始化 <see cref="T:System.Web.HttpContext"/> 类的新实例。
        /// </summary>
        /// <param name="request">当前 HTTP 请求的 <see cref="T:System.Web.HttpRequest"/> 对象。</param><param name="response">当前 HTTP 请求的 <see cref="T:System.Web.HttpResponse"/> 对象。</param>
        public HttpContext(HttpRequest request, HttpResponse response);
        /// <summary>
        /// 使用指定的辅助请求对象初始化 <see cref="T:System.Web.HttpContext"/> 类的新实例。
        /// </summary>
        /// <param name="wr">当前 HTTP 请求的 <see cref="T:System.Web.HttpWorkerRequest"/> 对象。</param>
        public HttpContext(HttpWorkerRequest wr);
        /// <summary>
        /// 接受一个使用指定的用户函数的 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 请求。
        /// </summary>
        /// <param name="userFunc">用户函数。</param><exception cref="T:System.ArgumentNullException"><paramref name="userFunc"/> 参数为 null。</exception><exception cref="T:System.NotSupportedException">该请求不是一个<see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 请求。</exception>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc);
        /// <summary>
        /// 接受一个使用指定的用户函数和选项对象 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 请求。
        /// </summary>
        /// <param name="userFunc">用户函数。</param><param name="options">选项对象。</param><exception cref="T:System.ArgumentNullException"><paramref name="userFunc"/> 参数为 null。</exception><exception cref="T:System.NotSupportedException">该请求不是一个<see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 请求。</exception>
        public void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc, AspNetWebSocketOptions options);
        /// <summary>
        /// 引发在请求的 HTTP 部件结束时发生的虚拟事件。
        /// </summary>
        /// 
        /// <returns>
        /// 订阅标记。
        /// </returns>
        /// <param name="callback">HTTP 上下文对象。</param><exception cref="T:System.ArgumentNullException"><paramref name="callback"/> 参数为 null。</exception>
        public ISubscriptionToken AddOnRequestCompleted(Action<HttpContext> callback);
        /// <summary>
        /// 启动一个对象 <see cref="M:System.IDisposable.Dispose"/> 方法的调用,当此请求 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 的连接部分完成时。
        /// </summary>
        /// 
        /// <returns>
        /// 订阅标记。
        /// </returns>
        /// <param name="target">当该请求的 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 部分完成时其 <see cref="M:System.IDisposable.Dispose"/> 方法必须被调用的对象。</param>
        public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target);
        /// <summary>
        /// 返回当前服务类型的对象。
        /// </summary>
        /// 
        /// <returns>
        /// 如果找到服务,则为 <see cref="T:System.Web.HttpContext"/>;否则为 null。
        /// </returns>
        /// <param name="service">要将服务提供商设置为的 <see cref="T:System.Web.HttpContext"/> 服务类型。</param>
        object IServiceProvider.GetService(Type service);
        /// <summary>
        /// 用于为请求指定处理程序。
        /// </summary>
        /// <param name="handler">应处理请求的对象。</param><exception cref="T:System.InvalidOperationException"><see cref="M:System.Web.HttpContext.RemapHandler(System.Web.IHttpHandler)"/> 方法是在发生 <see cref="E:System.Web.HttpApplication.MapRequestHandler"/> 事件之后调用的。</exception>
        public void RemapHandler(IHttpHandler handler);
        /// <summary>
        /// 将异常添加到当前 HTTP 请求的异常集合中。
        /// </summary>
        /// <param name="errorInfo">要添加到异常集合中的 <see cref="T:System.Exception"/></param>
        public void AddError(Exception errorInfo);
        /// <summary>
        /// 清除当前 HTTP 请求的所有错误。
        /// </summary>
        public void ClearError();
        /// <summary>
        /// 设置支持 HTTP 请求所需的会话状态行为的类型。
        /// </summary>
        /// <param name="sessionStateBehavior">枚举值之一,该值指定需要什么类型的会话状态行为。</param><exception cref="T:System.InvalidOperationException">该方法在引发 <see cref="E:System.Web.HttpApplication.AcquireRequestState"/> 事件之后调用。</exception>
        public void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior);
        /// <summary>
        /// 为当前应用程序返回请求的配置信息。
        /// </summary>
        /// 
        /// <returns>
        /// 包含配置信息的对象。 (在使用之前,将返回的配置节强制转换为适当的配置类型。)
        /// </returns>
        /// <param name="name">为其请求信息的应用程序配置标记。</param>
        [Obsolete("The recommended alternative is System.Web.Configuration.WebConfigurationManager.GetWebApplicationSection in System.Web.dll. http://go.microsoft.com/fwlink/?linkid=14202")]
        public static object GetAppConfig(string name);
        /// <summary>
        /// 为当前 HTTP 请求返回所请求的配置信息。
        /// </summary>
        /// 
        /// <returns>
        /// 如果该节不存在,则为指定的 <see cref="T:System.Configuration.ConfigurationSection"/>null,如果该节不能在运行时访问,则为一个内部对象。 (在使用之前,将返回的对象强制转换为适当的配置类型。)
        /// </returns>
        /// <param name="name">为其请求信息的配置标记。</param>
        [Obsolete("The recommended alternative is System.Web.HttpContext.GetSection in System.Web.dll. http://go.microsoft.com/fwlink/?linkid=14202")]
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public object GetConfig(string name);
        /// <summary>
        /// 获取当前应用程序的默认配置的指定配置节。
        /// </summary>
        /// 
        /// <returns>
        /// 如果该节不存在,则为指定的 <see cref="T:System.Configuration.ConfigurationSection"/>null,如果该节不能在运行时访问,则为一个内部对象。
        /// </returns>
        /// <param name="sectionName">配置节路径(使用 XPath 格式)和配置元素名称。</param>
        public object GetSection(string sectionName);
        /// <summary>
        /// 使用给定路径重写 URL。
        /// </summary>
        /// <param name="path">内部重写路径。</param><exception cref="T:System.ArgumentNullException"><paramref name="path"/> 参数为 null。</exception><exception cref="T:System.Web.HttpException"><paramref name="path"/> 参数不在当前应用程序的根目录中。</exception>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public void RewritePath(string path);
        /// <summary>
        /// 使用给定路径和一个布尔值重写 URL,该布尔值用于指定是否修改服务器资源的虚拟路径。
        /// </summary>
        /// <param name="path">内部重写路径。</param><param name="rebaseClientPath">如果重置虚拟路径,则为 true;如果保持虚拟路径不变,则为 false。</param><exception cref="T:System.ArgumentNullException"><paramref name="path"/> 参数为 null。</exception><exception cref="T:System.Web.HttpException"><paramref name="path"/> 参数不在当前应用程序的根目录中。</exception>
        public void RewritePath(string path, bool rebaseClientPath);
        /// <summary>
        /// 使用给定的路径、路径信息和查询字符串信息重写 URL。
        /// </summary>
        /// <param name="filePath">内部重写路径。</param><param name="pathInfo">资源的附加路径信息。 有关更多信息,请参见 <see cref="P:System.Web.HttpRequest.PathInfo"/></param><param name="queryString">请求查询字符串。</param><exception cref="T:System.ArgumentNullException"><paramref name="path"/> 参数不在当前应用程序的根目录中。</exception><exception cref="T:System.Web.HttpException"><paramref name="filePath"/> 参数不在当前应用程序的根目录中。</exception>
        public void RewritePath(string filePath, string pathInfo, string queryString);
        /// <summary>
        /// 使用给定虚拟路径、路径信息、查询字符串信息和一个布尔值重写 URL,该布尔值用于指定是否将客户端文件路径设置为重写路径。
        /// </summary>
        /// <param name="filePath">服务请求的资源的虚拟路径。</param><param name="pathInfo">用于 URL 重定向的附加路径信息。 有关更多信息,请参见 <see cref="P:System.Web.HttpRequest.PathInfo"/></param><param name="queryString">用于 URL 重定向的请求查询字符串。</param><param name="setClientFilePath">若要将用于客户端资源的文件路径设置为 <paramref name="filePath"/> 参数的值,则为 true;否则为 false。</param><exception cref="T:System.ArgumentNullException"><paramref name="path"/> 参数不在当前应用程序的根目录中。</exception><exception cref="T:System.Web.HttpException"><paramref name="filePath"/> 参数不在当前应用程序的根目录中。</exception>
        public void RewritePath(string filePath, string pathInfo, string queryString, bool setClientFilePath);
        /// <summary>
        /// 根据指定的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ClassKey"/><see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性,获取应用程序级别资源对象。
        /// </summary>
        /// 
        /// <returns>
        /// 一个 <see cref="T:System.Object"/>,表示请求的应用程序级别资源对象;否则,如果未找到资源对象,或找到了资源对象但其没有请求的属性,则为 null。
        /// </returns>
        /// <param name="classKey">一个字符串,表示所请求资源对象的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ClassKey"/> 属性。</param><param name="resourceKey">一个字符串,表示所请求资源对象的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性。</param><exception cref="T:System.Resources.MissingManifestResourceException">未找到具有指定 <paramref name="classKey"/> 参数的资源对象。 - 或 - 主程序集不包含非特定区域性的资源,而由于缺少相应的附属程序集,这些资源又是必需的。</exception>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static object GetGlobalResourceObject(string classKey, string resourceKey);
        /// <summary>
        /// 根据指定的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ClassKey"/><see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性以及 <see cref="T:System.Globalization.CultureInfo"/> 对象,获取一个应用程序级别资源对象。
        /// </summary>
        /// 
        /// <returns>
        /// 一个 <see cref="T:System.Object"/>,表示请求的应用程序级别资源对象(对指定区域性对其进行了本地化);否则,如果未找到资源对象,或找到了资源对象但其没有请求的属性,则为 null。
        /// </returns>
        /// <param name="classKey">一个字符串,表示所请求资源对象的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ClassKey"/> 属性。</param><param name="resourceKey">一个字符串,表示所请求资源对象的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性。</param><param name="culture">一个字符串,表示请求的资源的 <see cref="T:System.Globalization.CultureInfo"/> 对象。</param><exception cref="T:System.Resources.MissingManifestResourceException">未找到具有指定 <paramref name="classKey"/> 参数的资源对象。 - 或 - 主程序集不包含非特定区域性的资源,而由于缺少相应的附属程序集,这些资源又是必需的。</exception>
        public static object GetGlobalResourceObject(string classKey, string resourceKey, CultureInfo culture);
        /// <summary>
        /// 根据指定的 <see cref="P:System.Web.Compilation.ExpressionBuilderContext.VirtualPath"/><see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性,获取页级别资源对象。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Object"/>,表示请求的页级别资源对象;否则,如果找到了匹配的资源对象但不是 <paramref name="resourceKey"/> 参数,则为 null。
        /// </returns>
        /// <param name="virtualPath">本地资源对象的 <see cref="P:System.Web.Compilation.ExpressionBuilderContext.VirtualPath"/> 属性。</param><param name="resourceKey">一个字符串,表示所请求资源对象的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性。</param><exception cref="T:System.Resources.MissingManifestResourceException">未找到具有指定 <paramref name="virtualPath"/> 参数的资源对象。</exception><exception cref="T:System.ArgumentException">指定的 <paramref name="virtualPath"/> 参数不在当前应用程序的根目录中。</exception><exception cref="T:System.InvalidOperationException">未能找到页的资源类。</exception>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static object GetLocalResourceObject(string virtualPath, string resourceKey);
        /// <summary>
        /// 根据指定的 <see cref="P:System.Web.Compilation.ExpressionBuilderContext.VirtualPath"/><see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性以及 <see cref="T:System.Globalization.CultureInfo"/> 对象,获取一个页级别资源对象。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Object"/>,表示请求的本地资源对象(针对指定区域性进行了本地化的对象);否则,如果找到了匹配的资源对象但不是 <paramref name="resourceKey"/> 参数,则为 null。
        /// </returns>
        /// <param name="virtualPath">本地资源对象的 <see cref="P:System.Web.Compilation.ExpressionBuilderContext.VirtualPath"/> 属性。</param><param name="resourceKey">一个字符串,表示所请求资源对象的 <see cref="P:System.Web.Compilation.ResourceExpressionFields.ResourceKey"/> 属性。</param><param name="culture">一个字符串,表示所请求资源对象的 <see cref="T:System.Globalization.CultureInfo"/> 对象。</param><exception cref="T:System.Resources.MissingManifestResourceException">未找到具有指定 <paramref name="virtualPath"/> 参数的资源对象。</exception><exception cref="T:System.ArgumentException">指定的 <paramref name="virtualPath"/> 参数不在当前应用程序的根目录中。</exception><exception cref="T:System.InvalidOperationException">未能找到页的资源类。</exception>
        public static object GetLocalResourceObject(string virtualPath, string resourceKey, CultureInfo culture);
        /// <summary>
        /// 获取一个值,该值指示此请求是否是一个 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 请求。
        /// </summary>
        /// 
        /// <returns>
        /// 如果该请求是一 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/>请求,则为 true,否则为 false。
        /// </returns>
        public bool IsWebSocketRequest { get; }
        /// <summary>
        /// 获取指示连接是否从 HTTP连接升级到 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 连接的值。
        /// </summary>
        /// 
        /// <returns>
        /// 如果连接正在升级,则为 true;否则为 false。
        /// </returns>
        public bool IsWebSocketRequestUpgrading { get; }
        /// <summary>
        /// 获取客户要求的子协议列表的有序列表。
        /// </summary>
        /// 
        /// <returns>
        /// 为请求的协议,或如果不是 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 请求,或者列表不存在则为 null。
        /// </returns>
        public IList<string> WebSocketRequestedProtocols { get; }
        /// <summary>
        /// 获取用于 <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> 连接而从服务器发送到客户端的协商协议。
        /// </summary>
        /// 
        /// <returns>
        /// 协商的协议。
        /// </returns>
        public string WebSocketNegotiatedProtocol { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; }
        /// <summary>
        /// 为当前 HTTP 请求获取或设置 <see cref="T:System.Web.HttpContext"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Web.HttpContext"/>/// </returns>
        public static HttpContext Current { get; set; }
        /// <summary>
        /// 获取或设置包含适用于异步预加载模式的标志的对象。
        /// </summary>
        /// 
        /// <returns>
        /// 包含适用于异步预加载模式的标志的对象。
        /// </returns>
        public AsyncPreloadModeFlags AsyncPreloadMode { get; set; }
        /// <summary>
        /// 获取或设置一个值,此值指示 ASP.NET 请求处理过程中是否允许非预期的异步操作。
        /// </summary>
        /// 
        /// <returns>
        /// 当异步 API 在不是预期的时间使用时 ASP.NET 将引发异常,则 false,否则为 true。 默认值为 false。
        /// </returns>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public bool AllowAsyncDuringSyncStages { get; set; }
        /// <summary>
        /// 为当前 HTTP 请求获取或设置 <see cref="T:System.Web.HttpApplication"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Web.HttpApplication"/>。 ASP.NET 使用 ApplicationInstance 代替 Application 作为属性名来指代当前的 <see cref="T:System.Web.HttpApplication"/> 实例,以避免 ASP.NET 与经典 ASP 之间产生混淆。 在经典 ASP 中,Application 指代全局应用程序状态字典。
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">Web 应用程序正在 IIS 7.0 集成模式下运行,并且已尝试将属性值从非 NULL 值更改为 null。</exception>
        public HttpApplication ApplicationInstance { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; set; }
        /// <summary>
        /// 为当前 HTTP 请求获取 <see cref="T:System.Web.HttpApplicationState"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Web.HttpApplicationState"/>。 若要获取当前 HTTP 请求的 <see cref="T:System.Web.HttpApplication"/> 对象,请使用 <see cref="P:System.Web.HttpContext.ApplicationInstance"/>。 (ASP.NET 使用 ApplicationInstance 代替 Application 作为属性名来指代当前的 <see cref="T:System.Web.HttpApplication"/> 实例,以避免 ASP.NET 与经典 ASP 之间产生混淆。 在经典 ASP 中,Application 指代全局应用程序状态字典。)
        /// </returns>
        public HttpApplicationState Application { get; }
        /// <summary>
        /// 获取或设置负责处理 HTTP 请求的 <see cref="T:System.Web.IHttpHandler"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 负责处理 HTTP 请求的 <see cref="T:System.Web.IHttpHandler"/>/// </returns>
        public IHttpHandler Handler { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; set; }
        /// <summary>
        /// 获取父处理程序的 <see cref="T:System.Web.IHttpHandler"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Web.IHttpHandler"/> 实例,如果未找到上一个处理程序,则为 null。
        /// </returns>
        public IHttpHandler PreviousHandler { get; }
        /// <summary>
        /// 获取表示当前正在执行的处理程序的 <see cref="T:System.Web.IHttpHandler"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 一个 <see cref="T:System.Web.IHttpHandler"/>,表示当前正在执行的处理程序。
        /// </returns>
        public IHttpHandler CurrentHandler { get; }
        /// <summary>
        /// 获取当前 HTTP 请求的 <see cref="T:System.Web.HttpRequest"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Web.HttpRequest"/>/// </returns>
        /// <exception cref="T:System.Web.HttpException">Web 应用程序正在 IIS 7 的“集成”模式下运行。</exception>
        public HttpRequest Request { [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] get; }
        /// <summary>
        /// 获取当前 HTTP 响应的 <see cref="T:System.Web.HttpResponse"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 响应的 <see cref="T:System.Web.HttpResponse"/>/// </returns>
        /// <exception cref="T:System.Web.HttpException">Web 应用程序正在 IIS 7 的“集成”模式下运行。</exception>
        public HttpResponse Response { [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] get; }
        /// <summary>
        /// 为当前 HTTP 响应获取 <see cref="T:System.Web.TraceContext"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 响应的 <see cref="T:System.Web.TraceContext"/>/// </returns>
        public TraceContext Trace { get; }
        /// <summary>
        /// 获取可用于在 HTTP 请求过程中在 <see cref="T:System.Web.IHttpModule"/> 接口和 <see cref="T:System.Web.IHttpHandler"/> 接口之间组织和共享数据的键/值集合。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Collections.IDictionary"/> 键/值集合,按指定的键提供对集合中个别值的访问。
        /// </returns>
        public IDictionary Items { get; }
        /// <summary>
        /// 为当前 HTTP 请求获取 <see cref="T:System.Web.SessionState.HttpSessionState"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Web.SessionState.HttpSessionState"/> 对象。
        /// </returns>
        public HttpSessionState Session { get; }
        /// <summary>
        /// 获取提供用于处理 Web 请求的方法的 <see cref="T:System.Web.HttpServerUtility"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Web.HttpServerUtility"/>/// </returns>
        public HttpServerUtility Server { get; }
        /// <summary>
        /// 获取在处理 HTTP 请求的过程中累积的第一个错误(如果有)。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求/响应过程的第一个 <see cref="T:System.Exception"/>;如果在处理 HTTP 请求期间没有发生任何错误,则为 null。 默认值为 null。
        /// </returns>
        public Exception Error { get; }
        /// <summary>
        /// 获取在处理 HTTP 请求的过程中累积的错误数组。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的 <see cref="T:System.Exception"/> 对象数组。
        /// </returns>
        public Exception[] AllErrors { get; }
        /// <summary>
        /// 为当前 HTTP 请求获取或设置安全信息。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的安全信息。
        /// </returns>
        public IPrincipal User { get; [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries"), SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)] set; }
        /// <summary>
        /// 获取当前用户配置文件的 <see cref="T:System.Web.Profile.ProfileBase"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 如果应用程序配置文件包含配置文件的属性的定义,则为 <see cref="T:System.Web.Profile.ProfileBase"/>;否则为 null。
        /// </returns>
        public ProfileBase Profile { get; }
        /// <summary>
        /// 获取或设置一个值,该值指定 <see cref="T:System.Web.Security.UrlAuthorizationModule"/> 对象是否应跳过对当前请求的授权检查。
        /// </summary>
        /// 
        /// <returns>
        /// 如果 <see cref="T:System.Web.Security.UrlAuthorizationModule"/> 应跳过授权检查,则为 true;否则为 false。 默认值为 false。
        /// </returns>
        public bool SkipAuthorization { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries"), SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)] set; }
        /// <summary>
        /// 获取一个值,该值指示当前 HTTP 请求是否处于调试模式。
        /// </summary>
        /// 
        /// <returns>
        /// 如果请求处于调试模式,则为 true;否则,为 false。
        /// </returns>
        public bool IsDebuggingEnabled { get; }
        /// <summary>
        /// 获取一个值,该值指示是否已为当前 HTTP 请求启用自定义错误。
        /// </summary>
        /// 
        /// <returns>
        /// 如果启用了自定义错误,则为 true;否则,为 false。
        /// </returns>
        public bool IsCustomErrorEnabled { get; }
        /// <summary>
        /// 获取当前 HTTP 请求的初始时间戳。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 HTTP 请求的时间戳。
        /// </returns>
        public DateTime Timestamp { get; }
        /// <summary>
        /// 获取当前应用程序域的 <see cref="T:System.Web.Caching.Cache"/> 对象。
        /// </summary>
        /// 
        /// <returns>
        /// 当前应用程序域的 <see cref="T:System.Web.Caching.Cache"/>/// </returns>
        public Cache Cache { get; }
        /// <summary>
        /// 获取此要求的页检测服务实例的引用。
        /// </summary>
        /// 
        /// <returns>
        /// 此请求的页检测服务实例。
        /// </returns>
        public PageInstrumentationService PageInstrumentation { get; }
        /// <summary>
        /// 获取或设置一个值,此值指示 ASP.NET 运行时是否应在请求超时时,调用为该请求提供服务的线程上的 <see cref="M:System.Threading.Thread.Abort"/>/// </summary>
        /// 
        /// <returns>
        /// true 如果 <see cref="M:System.Threading.Thread.Abort"/> 将调用,当线程时间;否则, false。 默认值为 true。
        /// </returns>
        public bool ThreadAbortOnTimeout { get; set; }
        /// <summary>
        /// 获取一个 <see cref="T:System.Web.RequestNotification"/> 值,该值指示当前正在处理的 <see cref="T:System.Web.HttpApplication"/> 事件。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Web.RequestNotification"/> 值之一。
        /// </returns>
        /// <exception cref="T:System.PlatformNotSupportedException">操作要求 IIS 7 处于集成管线模式,并且要求 .NET Framework 至少为 3.0 版本。</exception>
        public RequestNotification CurrentNotification { get; internal set; }
        /// <summary>
        ///<see cref="T:System.Web.HttpApplication"/> 事件完成处理之后获取 ASP.NET 管道的当前进程点的值。
        /// </summary>
        /// 
        /// <returns>
        /// 如果启用了自定义错误,则为 true;否则,为 false。
        /// </returns>
        /// <exception cref="T:System.PlatformNotSupportedException">操作要求 IIS 7 中的集成管线模式并且要求为 .NET Framework 3.0。</exception>
        public bool IsPostNotification { get; internal set; }
      }
      /// <summary>
      /// 提供对会话状态值以及会话级别设置和生存期管理方法的访问。
      /// </summary>
      public sealed class HttpSessionState : ICollection, IEnumerable
      {
        /// <summary>
        /// 取消当前会话。
        /// </summary>
        public void Abandon();
        /// <summary>
        /// 向会话状态集合添加一个新项。
        /// </summary>
        /// <param name="name">要添加到会话状态集合的项的名称。</param><param name="value">要添加到会话状态集合的项的值。</param>
        public void Add(string name, object value);
        /// <summary>
        /// 删除会话状态集合中的项。
        /// </summary>
        /// <param name="name">要从会话状态集合中删除的项的名称。</param>
        public void Remove(string name);
        /// <summary>
        /// 删除会话状态集合中指定索引处的项。
        /// </summary>
        /// <param name="index">要从会话状态集合中移除的项的索引。</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> 小于零。 - 或 - <paramref name="index"/> 等于或大于 <see cref="P:System.Web.SessionState.HttpSessionState.Count"/></exception>
        public void RemoveAt(int index);
        /// <summary>
        /// 从会话状态集合中移除所有的键和值。
        /// </summary>
        public void Clear();
        /// <summary>
        /// 从会话状态集合中移除所有的键和值。
        /// </summary>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public void RemoveAll();
        /// <summary>
        /// 返回一个枚举数,可用来读取当前会话中所有会话状态的变量名称。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Collections.IEnumerator"/> 可以循环访问会话状态集合中的变量名称。
        /// </returns>
        public IEnumerator GetEnumerator();
        /// <summary>
        /// 将会话状态值的集合复制到一维数组中(从数组的指定索引处开始)。
        /// </summary>
        /// <param name="array"><see cref="T:System.Array"/>,它接收会话值。</param><param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param>
        public void CopyTo(Array array, int index);
        /// <summary>
        /// 获取会话的唯一标识符。
        /// </summary>
        /// 
        /// <returns>
        /// 唯一会话标识符。
        /// </returns>
        public string SessionID { get; }
        /// <summary>
        /// 获取并设置在会话状态提供程序终止会话之前各请求之间所允许的时间(以分钟为单位)。
        /// </summary>
        /// 
        /// <returns>
        /// 超时期限(以分钟为单位)。
        /// </returns>
        public int Timeout { get; set; }
        /// <summary>
        /// 获取一个值,该值指示会话是否是与当前请求一起创建的。
        /// </summary>
        /// 
        /// <returns>
        /// 如果会话是与当前请求一起创建的,则为 true;否则,为 false。
        /// </returns>
        public bool IsNewSession { get; }
        /// <summary>
        /// 获取当前会话状态模式。
        /// </summary>
        /// 
        /// <returns>
        /// <see cref="T:System.Web.SessionState.SessionStateMode"/> 值之一。
        /// </returns>
        public SessionStateMode Mode { get; }
        /// <summary>
        /// 获取一个值,该值指示会话 ID 是嵌入在 URL 中还是存储在 HTTP Cookie 中。
        /// </summary>
        /// 
        /// <returns>
        /// 如果会话嵌入在 URL 中,则为 true;否则,为 false。
        /// </returns>
        public bool IsCookieless { get; }
        /// <summary>
        /// 获取一个值,该值指示是否为无 Cookie 会话配置应用程序。
        /// </summary>
        /// 
        /// <returns>
        /// 指示是否为无 Cookie 会话配置应用程序的其中一个 <see cref="T:System.Web.HttpCookieMode"/> 值。 默认值为 <see cref="F:System.Web.HttpCookieMode.UseCookies"/>/// </returns>
        public HttpCookieMode CookieMode { get; }
        /// <summary>
        /// 获取或设置当前会话的区域设置标识符 (LCID)。
        /// </summary>
        /// 
        /// <returns>
        /// 指定当前会话的区域性的 <see cref="T:System.Globalization.CultureInfo"/> 实例。
        /// </returns>
        public int LCID { get; set; }
        /// <summary>
        /// 获取或设置当前会话的字符集标识符。
        /// </summary>
        /// 
        /// <returns>
        /// 当前会话的字符集标识符。
        /// </returns>
        public int CodePage { get; set; }
        /// <summary>
        /// 获取对当前会话状态对象的引用。
        /// </summary>
        /// 
        /// <returns>
        /// 当前 <see cref="T:System.Web.SessionState.HttpSessionState"/>/// </returns>
        public HttpSessionState Contents { get; }
        /// <summary>
        /// 获取由 ASP.NET 应用程序文件 Global.asax 中的 &lt;object Runat="Server" Scope="Session"/&gt; 标记声明的对象的集合。
        /// </summary>
        /// 
        /// <returns>
        /// 一个 <see cref="T:System.Web.HttpStaticObjectsCollection"/>,它包含在 Global.asax 文件中声明的对象。
        /// </returns>
        public HttpStaticObjectsCollection StaticObjects { get; }
        /// <summary>
        /// 按名称获取或设置会话值。
        /// </summary>
        /// 
        /// <returns>
        /// 具有指定名称的会话状态值;如果该项不存在,则为 null。
        /// </returns>
        /// <param name="name">会话值的键名。</param>
        public object this[string name] { get; set; }
        /// <summary>
        /// 按数字索引获取或设置会话值。
        /// </summary>
        /// 
        /// <returns>
        /// 存储在指定索引处的会话状态值;如果该项不存在,则为 null。
        /// </returns>
        /// <param name="index">会话值的数字索引。</param>
        public object this[int index] { get; set; }
        /// <summary>
        /// 获取会话状态集合中的项数。
        /// </summary>
        /// 
        /// <returns>
        /// 集合中的项数。
        /// </returns>
        public int Count { get; }
        /// <summary>
        /// 获取存储在会话状态集合中所有值的键的集合。
        /// </summary>
        /// 
        /// <returns>
        /// 包含所有会话键的 <see cref="T:System.Collections.Specialized.NameObjectCollectionBase.KeysCollection"/>/// </returns>
        public NameObjectCollectionBase.KeysCollection Keys { get; }
        /// <summary>
        /// 获取一个对象,该对象可用于同步对会话状态值的集合的访问。
        /// </summary>
        /// 
        /// <returns>
        /// 可用于同步集合访问的对象。
        /// </returns>
        public object SyncRoot { get; }
        /// <summary>
        /// 获取一个值,该值指示会话是否为只读。
        /// </summary>
        /// 
        /// <returns>
        /// 如果会话为只读,则为 true;否则为 false。
        /// </returns>
        public bool IsReadOnly { get; }
        /// <summary>
        /// 获取一个值,该值指示对会话状态值的集合的访问是否是同步(线程安全)的。
        /// </summary>
        /// 
        /// <returns>
        /// 如果对该集合的访问是同步的(线程安全),则为 true;否则,为 false。
        /// </returns>
        public bool IsSynchronized { get; }
      }

        注:较多的.net程序员在项目开发和学习中,较少去直接查看.net提供的源码,在查看源码的过程中,会发现该类实现的一些接口和继承的抽象类,有助于我们更好的了解该类的特性。

       

  • 相关阅读:
    SharePoint Framework (SPFx) 开发入门教程
    SharePoint 2013 Designer 入门教程
    SharePoint 2013 开发教程
    SharePoint 2013 入门教程
    SharePoint Online 部署SPFx Web部件
    SharePoint Online SPFx Web部件绑定数据
    SharePoint Online 创建SPFx客户端Web部件
    SharePoint Online 配置框架(SPFx)开发环境
    SharePoint Online 创建应用程序目录
    SharePoint Online 启用 IRM
  • 原文地址:https://www.cnblogs.com/pengze0902/p/5944533.html
Copyright © 2020-2023  润新知