立即注册 找回密码

QQ登录

只需一步,快速开始

查看: 3392|回复: 0

[通用使用教程] web.config配置文件使用总结的方法

[复制链接]
发表于 2020-10-10 12:12:56 | 显示全部楼层 |阅读模式
道勤网-数据www.daoqin.net

亲注册登录道勤网-可以查看更多帖子内容哦!(包涵精彩图片、文字详情等)请您及时注册登录-www.daoqin.net

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我们在开发web系统的时候,使用web.config进行配置是司空见惯的,那么web.confg到底是什么呢?什么时候使用web.config呢?有几种使用web.config方式呢? 如果不太明白的话,那这篇文章就带领大家脑补下。
  》》首先回答第一个问题,web.config是什么?
  是一个XML文本文件。用来存储ASP.NETWEB的配置信息。修改不需要重启服务器就可以生效。
》》 然后第二个问题,什么时候使用web.config?
  网站中很多不确定的可能随时更改的数据都可以写到配置文件里面。
》》有几种使用web.config的方式呢?
下面就介绍几种常见的:
~ connectionStrings配置(这个一般用来配置数据库连接)
<connectionStrings>
    <add name="Default" connectionString="Server=.; Database=Inferno;User ID=sa; Password=pwd123;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
  </connectionStrings>

获取:
  1. <font size="2"> string dbStr=ConfigurationManager.ConnectionStrings["Default"].ConnectionString;</font>
复制代码
~appSettings配置(这个一般用来配置不确定的或者需要用户配置的内容)
  1. <font size="2"><appSettings>
  2.     <add key="webpages:Version" value="3.0.0.0" />
  3.     <add key="webpages:Enabled" value="false" />
  4.     <add key="ClientValidationEnabled" value="true" />
  5.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  6.   </appSettings></font>
复制代码
获取
  1. <font size="2">string clientValidationEnabled= ConfigurationManager.AppSettings["ClientValidationEnabled"].ToString();</font>
复制代码
~自定义配置(这个一般用户批量配置某些内容)
例如,我下面定义了一个jieDian 键值对配置和一个 fileupload 上传文件集合配置

web.config配置文件使用总结的方法

web.config配置文件使用总结的方法
其中,上面的UploadConditionHandler我对应的是一个类:(这个类需要继承IConfigurationSectionHandler并实现Create方法)
  1. <font size="2">namespace TestProject
  2. {
  3.     public class UploadConditionHandler : IConfigurationSectionHandler
  4.     {
  5.         public UploadConditionHandler() { }
  6.         public static NameValueCollection ossrules = ConfigurationManager.GetSection("testSectionGroup/jieDian") as NameValueCollection;

  7.         public static Dictionary<string, UploadRequiredParameters> ScreensDictionary = ConfigurationManager.GetSection("testSectionGroup/fileupload") as Dictionary<string, UploadRequiredParameters>;
  8.         public object Create(object parent, object configContext, XmlNode section)
  9.         {
  10.             Dictionary<string, UploadRequiredParameters> screens = new Dictionary<string, UploadRequiredParameters>();

  11.             try
  12.             {
  13.                 string name = string.Empty;
  14.                 string path_rule = string.Empty;
  15.                 string disk = string.Empty;
  16.                 string receive_server = string.Empty;
  17.                 string ext_array = string.Empty;
  18.                 string max_memory_size = string.Empty;
  19.                 string enum_bucket_name = string.Empty;
  20.                 string max_pixel_height = string.Empty;
  21.                 string max_pixel_width = string.Empty;
  22.                 foreach (XmlNode childNode in section.ChildNodes)
  23.                 {
  24.                     if (childNode.NodeType != XmlNodeType.Element || childNode.Name != "upload")
  25.                         continue;

  26.                     if (childNode.Attributes["name"] != null)
  27.                     {
  28.                         name = childNode.Attributes["name"].Value;
  29.                         if (childNode.Attributes["path_rule"] != null)
  30.                         {
  31.                             path_rule = childNode.Attributes["path_rule"].Value;
  32.                         }
  33.                         if (childNode.Attributes["disk"] != null)
  34.                         {
  35.                             disk = childNode.Attributes["disk"].Value;
  36.                         }
  37.                         if (childNode.Attributes["receive_server"] != null)
  38.                         {
  39.                             receive_server = childNode.Attributes["receive_server"].Value;
  40.                         }
  41.                         if (childNode.Attributes["ext_array"] != null)
  42.                         {
  43.                             ext_array = childNode.Attributes["ext_array"].Value;
  44.                         }
  45.                         if (childNode.Attributes["max_memory_size"] != null)
  46.                         {
  47.                             max_memory_size = childNode.Attributes["max_memory_size"].Value;
  48.                         }
  49.                         if (childNode.Attributes["enum_bucket_name"] != null)
  50.                         {
  51.                             enum_bucket_name = childNode.Attributes["enum_bucket_name"].Value;
  52.                         }
  53.                         if (childNode.Attributes["max_height"] != null)
  54.                         {
  55.                             max_pixel_height = childNode.Attributes["max_height"].Value;
  56.                         }
  57.                         if (childNode.Attributes["max_width"] != null)
  58.                         {
  59.                             max_pixel_width = childNode.Attributes["max_width"].Value;
  60.                         }
  61.                         UploadRequiredParameters upload = new UploadRequiredParameters();

  62.                         upload.PathRule = path_rule;
  63.                         upload.Disk = disk;
  64.                         upload.ReceiveServer = receive_server;
  65.                         upload.ExtArray = ext_array;

  66.                         int maxMemorySize = 0;
  67.                         if (int.TryParse(max_memory_size, out maxMemorySize))
  68.                         {
  69.                             upload.MaxMemorySize = maxMemorySize;
  70.                         }
  71.                         upload.EnumBucketName = enum_bucket_name;
  72.                         int maxPixelHeight = 0;
  73.                         if (int.TryParse(max_pixel_height, out maxPixelHeight))
  74.                         {
  75.                             upload.MaxPixelHeight = maxPixelHeight;
  76.                         }
  77.                         int maxPixelWidth = 0;
  78.                         if (int.TryParse(max_pixel_width, out maxPixelWidth))
  79.                         {
  80.                             upload.MaxPixelWidth = maxPixelWidth;
  81.                         }

  82.                         screens.Add(name, upload);
  83.                     }
  84.                 }
  85.             }
  86.             catch (Exception ex)
  87.             {
  88.                 throw ex;
  89.             }
  90.             return screens;
  91.         }
  92.     }
  93. }</font>
复制代码
好了,上面的步骤完成后,就可以在其它地方调用了:         
  1. <font size="3">string jieDian1=UploadConditionHandler.ossrules["JieDian1"];
  2. UploadRequiredParameters res = UploadConditionHandler.ScreensDictionary["UploadRules_Mostly"];</font>
复制代码
目前就先介绍这么多啦,有什么疑问的或者需要了解的欢迎留言~  这样就可以了,

道勤主机提供365天*24小时全年全天无休、实时在线、零等待的售后技术支持。竭力为您免费处理您在使用道勤主机过程中所遇到的一切问题! 如果您是道勤主机用户,那么您可以通过QQ【792472177】、售后QQ【59133755】、旺旺【诠释意念】、微信:q792472177免费电话、后台提交工单这些方式联系道勤主机客服! 如果您不是我们的客户也没问题,点击页面最右边的企业QQ在线咨询图标联系我们并购买后,我们为您免费进行无缝搬家服务,让您享受网站零访问延迟的迁移到道勤主机的服务!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

道勤网- 推荐内容!上一条 /2 下一条

!jz_fbzt! !jz_sgzt! !jz_xgzt! 快速回复 !jz_fhlb! !jz_lxwm! !jz_gfqqq!

关于我们|手机版|小黑屋|地图|【道勤网】-www.daoqin.net 软件视频自学教程|免费教程|自学电脑|3D教程|平面教程|影视动画教程|办公教程|机械设计教程|网站设计教程【道勤网】 ( 皖ICP备15000319号-1 )

GMT+8, 2024-4-26 06:05

Powered by DaoQin! X3.4 © 2016-2063 Dao Qin & 道勤科技

快速回复 返回顶部 返回列表