Example #1
0
        /// <summary>
        /// 获取system.web节点
        /// </summary>
        /// <typeparam name="T">配置节点类型</typeparam>
        /// <param name="sections">节点类型</param>
        /// <returns></returns>
        public static T GetSystemWebSection <T>(SystemWebSections sections) where T : class
        {
            switch (sections)
            {
            case SystemWebSections.Authentication:
                var authenticationSection = WebConfigurationManager.GetSection("system.web/authentication") as AuthenticationSection;
                return(authenticationSection as T);

            case SystemWebSections.Compilation:
                var compilationSection = WebConfigurationManager.GetSection("system.web/compilation") as CompilationSection;
                return(compilationSection as T);

            case SystemWebSections.CustomErrors:
                var customErrorsSection = WebConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
                return(customErrorsSection as T);

            case SystemWebSections.Globalization:
                var globalizationSection = WebConfigurationManager.GetSection("system.web/globalization") as GlobalizationSection;
                return(globalizationSection as T);

            case SystemWebSections.HttpRuntime:
                var httpRuntimeSection = WebConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
                return(httpRuntimeSection as T);

            case SystemWebSections.Identity:
                var identitySection = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection;
                return(identitySection as T);

            case SystemWebSections.Trace:
                var traceSection = WebConfigurationManager.GetSection("system.web/trace") as TraceSection;
                return(traceSection as T);

            default:
                return(default(T));
            }
        }
Example #2
0
        /// <summary>
        /// <para>Get section from system.web setcion on web.config / app.config</para>
        /// </summary>
        /// <typeparam name="T">Section class to return</typeparam>
        /// <param name="section">Section type</param>
        /// <returns>system.web section or null</returns>
        public static T GetFromSystemWeb <T>(SystemWebSections section) where T : class
        {
            try
            {
                switch (section)
                {
                case SystemWebSections.Authentication:
                    var authenticationSection = WebConfigurationManager.GetSection("system.web/authentication") as AuthenticationSection;

                    return(authenticationSection == null
                            ? default(T)
                            : authenticationSection as T);

                case SystemWebSections.Compilation:
                    var compilationSection = WebConfigurationManager.GetSection("system.web/compilation") as CompilationSection;

                    return(compilationSection == null
                            ? default(T)
                            : compilationSection as T);

                case SystemWebSections.CustomErrors:
                    var customErrorsSection = WebConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;

                    return(customErrorsSection == null
                            ? default(T)
                            : customErrorsSection as T);

                case SystemWebSections.Globalization:
                    var globalizationSection = WebConfigurationManager.GetSection("system.web/globalization") as GlobalizationSection;

                    return(globalizationSection == null
                            ? default(T)
                            : globalizationSection as T);

                case SystemWebSections.HttpRuntime:
                    var httpRuntimeSection = WebConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;

                    return(httpRuntimeSection == null
                            ? default(T)
                            : httpRuntimeSection as T);

                case SystemWebSections.Identity:
                    var identitySection = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection;

                    return(identitySection == null
                            ? default(T)
                            : identitySection as T);

                case SystemWebSections.Trace:
                    var traceSection = WebConfigurationManager.GetSection("system.web/trace") as TraceSection;

                    return(traceSection == null
                            ? default(T)
                            : traceSection as T);

                default:
                    return(default(T));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(null);
            }
        }