前言:
现在姐妹们对“net45webconfig”大致比较关怀,朋友们都需要学习一些“net45webconfig”的相关知识。那么小编同时在网上汇集了一些对于“net45webconfig””的相关资讯,希望兄弟们能喜欢,小伙伴们一起来学习一下吧!简单写了一个类,主要用来操作web.config中几个常用节的值
包括appsetting,ConnectionStrings,customErrors以及设置Mode和跳转网址
#region webconfig 文件public class WebConfig{ /// <summary> /// 操作 AppSetting 节 /// </summary> public class AppSetting { /// <summary> /// 得到 appsetting 节的值 返回null出错 /// <para>如 add key="a" value="b"/</para> /// <para>如 Get("a") 得到的值为 b </para> /// <para>返回null出错</para> /// </summary> /// <param name="nodename">节名称</param> /// <returns>节值,返回null出错</returns> public string Get(string nodename) { try { System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader(); if (ConfigurationManager.AppSettings[nodename] != null) { string str = Convert.ToString(appReader.GetValue(nodename, typeof(string))).Trim(); return str; } else { return null; } } catch (Exception ErrMsg) { return null; } } /// <summary> /// 设置 appsettiong 节的值,成功返回OK,否则返回错误信息 /// </summary> /// <param name="key">节名称</param> /// <param name="value">新值</param> public string Set(string key, string value) { try { Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings"); appSection.Settings[key].Value = value; config.Save(); return "OK"; } catch (Exception ErrMsg) { return ErrMsg.ToString(); } } } /// <summary> /// 操作 ConnectionStrings 节 /// </summary> public class ConnectionStrings { /// <summary> /// 得到 ConnectionStrings 节的值 返回null出错 /// <para>如 add name="Dbtype" connectionString="0" </para> /// <para>如 Get("Dbtype") 得到的值为 0 </para> /// <para>返回null出错</para> /// </summary> /// <param name="nodename">节名称</param> /// <returns>节值,返回null出错</returns> public string Get(string thename) { try { return System.Configuration.ConfigurationManager.ConnectionStrings[thename].ConnectionString; } catch (Exception ErrMsg) { return null; } } } /// <summary> /// 操作 customErrors节 /// <para>此节配置在 customerErrors -> configuration-> system.web 下</para> /// <para>如 customErrors defaultRedirect="defaultError.htm" mode="RemoteOnly"</para> /// <para></para> /// </summary> public class CustomError { /// <summary> /// 得到 customErrors 的 Mode 值 /// </summary> /// <returns></returns> public string Get_Mode() { try { Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); CustomErrorsSection customErrors = (CustomErrorsSection)config.GetSection("system.web/customErrors"); int mode = 1; switch (customErrors.Mode) { case CustomErrorsMode.On: mode = 0; break; case CustomErrorsMode.Off: mode = 1; break; case CustomErrorsMode.RemoteOnly: mode = 2; break; default: break; } return mode.ToString(); } catch (Exception ErrMsg) { return ErrMsg.ToString(); } } /// <summary> /// 设置 Mode 和 跳转网址 /// <para>Mode 0: 启用自定义错误。如果未指定 redirecturl,用户将看到一般性错误</para> /// </summary> /// <param name="thenum">Mode值 0:启用,1:禁用,2:调试时显示错误</param> /// <param name="redirecturl">出错跳转地址,""时显示一般错误</param> public string Set(int thenum, string redirecturl) { try { Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); CustomErrorsSection customErrors = (CustomErrorsSection)config.GetSection("system.web/customErrors"); switch (thenum) { case 0: customErrors.Mode = CustomErrorsMode.On; break; case 1: customErrors.Mode = CustomErrorsMode.Off; break; case 2: customErrors.Mode = CustomErrorsMode.RemoteOnly; break; default: customErrors.Mode = CustomErrorsMode.Off; break; } if (redirecturl.Length != 0) { customErrors.DefaultRedirect = redirecturl; } config.Save(); return "OK"; } catch (Exception ErrMsg) { return ErrMsg.ToString(); } } }}#endregion
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #net45webconfig