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

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

web.config配置文件使用总结的更多相关文章

  1. Web.config配置文件详解

    整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <?xml v ...

  2. asp.net项目中通过Web.config配置文件及文件夹的访问权限!

    描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面 ...

  3. [转]Web.config配置文件详解(新手必看)

    本文转自:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配 ...

  4. 转:Web.config配置文件详解(新手必看)

    转:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配置文件 ...

  5. Web.config配置文件

    优点:Web.config配置文件使得ASP.NET应用程序的配置变得灵活高效和容易实现并为ASP.NET应用提供了可扩展的配置,使得应用程序能够自定义配置,同时还包括的优点有:配置设置易读性.更新的 ...

  6. Web.config配置文件详解(新手必看)

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  7. .net Web.Config配置文件 转

    .net Web.Config配置文件 博客分类: .net   .net Web.Config配置文件 一.配置信息 <?xml version="1.0" encodin ...

  8. (转)Web.config配置文件详解(新手必看)

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  9. .NET Core迁移技巧之web.config配置文件

    大家都知道.NET Core现在不再支持原来的web.config配置文件了,取而代之的是json或xml配置文件.官方推荐的项目配置方式是使用appsettings.json配置文件,这对现有一些重 ...

  10. .NET Core 2.0迁移技巧之web.config配置文件

    大家都知道.NET Core现在不再支持原来的web.config配置文件了,取而代之的是json或xml配置文件.官方推荐的项目配置方式是使用appsettings.json配置文件,这对现有一些重 ...

随机推荐

  1. 第二次作业&熟悉使用工具

     GIT地址  我的地址  GIT用户名  995020892w  学号后五位  81105  博客地址  我的博客  作业链接  第二次作业 一.环境配置过程 安装vs2017 因为以前学习C#相关 ...

  2. hdu 2489 dfs枚举组合情况+最小生成树

    大家都说,搜索是算法的基础.今天最这题就有体会了.在n个顶点里选择m个顶点,求最小生成树.用到了深搜的回溯.所有情况都能枚举. #include<iostream> #include< ...

  3. mysql 主从错误情况与原因

    mysql 主从错误情况1,master 上删除一条记录是从库报错 找不到该记录引起原因:master出现宕机或者从库已经删除.解决方案:stop slave;set global sql_slave ...

  4. Zbrush 4R7中的镜像功能是怎么使用的?

    ZBrush一款3D图形绘制软件,功能十分强大,在雕刻建模的时候镜像工具是我们经常要用到的,它可以方便快捷雕刻对称的模型,那么ZBrush®中怎样镜像呢,本文小编将做详细介绍. ZBrush 3D镜像 ...

  5. matlab学习-使用自带的函数

    >> %定义矩阵求最大值>> a=[1 7 3;6 2 9];>> A=max(a);>> a a = 1 7 3 6 2 9 >> A A ...

  6. python编写简单的html登陆页面(1)

    1  html 打开调式效果如下 2  用python后台编写 # coding:utf-8# 从同一个位置导入多个工具,# 这些工具之间可以用逗号隔开,同时导入# render_template渲染 ...

  7. 16种C语言编译警告(Warning)类型的解决方法

    当编译程序发现程序中某个地方有疑问,可能有问题时就会给出一个警告信息.警告信息可能意味着程序中隐含的大错误,也可能确实没有问题.对于警告的正确处理方式应该是:尽可能地消除之.对于编译程序给出的每个警告 ...

  8. vim牛逼的code工具: ctags+ cscope

    自己总结 在我的工作目录里的.vimrc中做了这样的配置:   set tags=tags;   set autochdir 在项目根目录里利用"sudo ctags -R *", ...

  9. centos查看防火墙端口

    #centos7启动防火墙 systemctl start firewalld.service #centos7停止防火墙/关闭防火墙 systemctl stop firewalld.service ...

  10. 工具-Telerik trial安装图解