我们在开发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>

获取:

  string dbStr=ConfigurationManager.ConnectionStrings["Default"].ConnectionString;

~appSettings配置(这个一般用来配置不确定的或者需要用户配置的内容)

 <appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

获取

string clientValidationEnabled= ConfigurationManager.AppSettings["ClientValidationEnabled"].ToString();

~自定义配置(这个一般用户批量配置某些内容)

例如,我下面定义了一个jieDian 键值对配置和一个 fileupload 上传文件集合配置

其中,上面的UploadConditionHandler我对应的是一个类:(这个类需要继承IConfigurationSectionHandler并实现Create方法)

namespace TestProject
{
public class UploadConditionHandler : IConfigurationSectionHandler
{
public UploadConditionHandler() { }
public static NameValueCollection ossrules = ConfigurationManager.GetSection("testSectionGroup/jieDian") as NameValueCollection; public static Dictionary<string, UploadRequiredParameters> ScreensDictionary = ConfigurationManager.GetSection("testSectionGroup/fileupload") as Dictionary<string, UploadRequiredParameters>;
public object Create(object parent, object configContext, XmlNode section)
{
Dictionary<string, UploadRequiredParameters> screens = new Dictionary<string, UploadRequiredParameters>(); try
{
string name = string.Empty;
string path_rule = string.Empty;
string disk = string.Empty;
string receive_server = string.Empty;
string ext_array = string.Empty;
string max_memory_size = string.Empty;
string enum_bucket_name = string.Empty;
string max_pixel_height = string.Empty;
string max_pixel_width = string.Empty;
foreach (XmlNode childNode in section.ChildNodes)
{
if (childNode.NodeType != XmlNodeType.Element || childNode.Name != "upload")
continue; if (childNode.Attributes["name"] != null)
{
name = childNode.Attributes["name"].Value;
if (childNode.Attributes["path_rule"] != null)
{
path_rule = childNode.Attributes["path_rule"].Value;
}
if (childNode.Attributes["disk"] != null)
{
disk = childNode.Attributes["disk"].Value;
}
if (childNode.Attributes["receive_server"] != null)
{
receive_server = childNode.Attributes["receive_server"].Value;
}
if (childNode.Attributes["ext_array"] != null)
{
ext_array = childNode.Attributes["ext_array"].Value;
}
if (childNode.Attributes["max_memory_size"] != null)
{
max_memory_size = childNode.Attributes["max_memory_size"].Value;
}
if (childNode.Attributes["enum_bucket_name"] != null)
{
enum_bucket_name = childNode.Attributes["enum_bucket_name"].Value;
}
if (childNode.Attributes["max_height"] != null)
{
max_pixel_height = childNode.Attributes["max_height"].Value;
}
if (childNode.Attributes["max_width"] != null)
{
max_pixel_width = childNode.Attributes["max_width"].Value;
}
UploadRequiredParameters upload = new UploadRequiredParameters(); upload.PathRule = path_rule;
upload.Disk = disk;
upload.ReceiveServer = receive_server;
upload.ExtArray = ext_array; int maxMemorySize = ;
if (int.TryParse(max_memory_size, out maxMemorySize))
{
upload.MaxMemorySize = maxMemorySize;
}
upload.EnumBucketName = enum_bucket_name;
int maxPixelHeight = ;
if (int.TryParse(max_pixel_height, out maxPixelHeight))
{
upload.MaxPixelHeight = maxPixelHeight;
}
int maxPixelWidth = ;
if (int.TryParse(max_pixel_width, out maxPixelWidth))
{
upload.MaxPixelWidth = maxPixelWidth;
} screens.Add(name, upload);
}
}
}
catch (Exception ex)
{
throw ex;
}
return screens;
}
}
}

好了,上面的步骤完成后,就可以在其它地方调用了:

string jieDian1=UploadConditionHandler.ossrules["JieDian1"];
UploadRequiredParameters res = UploadConditionHandler.ScreensDictionary["UploadRules_Mostly"];

目前就先介绍这么多啦,有什么疑问的或者需要了解的欢迎留言~

最新文章

  1. .NET开发笔记(二十三) 谷歌地图下载
  2. String basePath = request.getScheme()+&quot;://&quot;+request.getServerName()+&quot;:&quot;+request.getServerPort()+pat----------&lt;base&gt;元素有关
  3. tomcat启动闪退
  4. protobuf 文件级别优化
  5. 多线程 or 多进程 (转强力推荐)
  6. docker-compose常用命令
  7. Jquery实现循环删除Reaper某一行
  8. 写了个Linux包过滤防火墙
  9. Unix网络编程(1)&mdash;&mdash;socket一窥
  10. javascript学习笔记2
  11. nyoj 517 最小公倍数 【java睑板】
  12. 在 Windows 上测试 Redis Cluster的集群填坑笔记
  13. 海量数据挖掘MMDS week3:社交网络之社区检测:基本技巧
  14. 落入绝地求生的Python神仙,实现绝地求生无后座!
  15. Java Web 获取客户端真实IP
  16. RNN 网络
  17. python接口测试-充值
  18. linux的cat命令
  19. parameter &quot;timeout&quot; in socketchannel does not work
  20. Owin+ASP.NET Identity浅析系列(四)实现用户角色

热门文章

  1. javascript学习中自己对作用域和作用域链理解
  2. 管窥python语法
  3. SQLServer之merge函数用法
  4. apiCloud组件:swiper
  5. day25-3 json,pickle模块
  6. hibernate注解--@transient
  7. spring注解@Autowired和@Resource比较
  8. Code VS 1002 搭桥
  9. 3.2、使用Flask-Bootstrap集成Twitter Bootstrap
  10. Hibernate Session操作