ASP.NET 配置文件

1.ASP.NET配置文件采用继承关系,其关系是:是machine.config-->web.config-->文件夹web.config。

2.关于<location/>节点节点,通常在配置权限时候,我们使用web.config对网站目录下文件夹或文件进行一个权限的限定访问,如:

  
    
<!-- 表示对文件夹admin1进行限制 -->
< location path = " admin1 " >
< system.web >
< authorization >
< allow roles = " -100,1,2 " />
< deny users = " * " />
</ authorization >
</ system.web >
</ location >

<!-- 表示对文件夹admin2以及文件admin2.aspx进行限制 -->
< location path = " admin2\admin2.aspx " >
< system.web >
< authorization >
< allow roles = " -100,3,4 " />
< deny users = " * " />
</ authorization >
</ system.web >
</ location >

3.关于lockAttributes,lockElements,lockAllAttributesExcept,lockAllElementsExcept属性,其表示锁定某个属性或节点,继承于其配置文件的将不能重写该属性,如:

  
    
< membership lockElements = " providers "
lockAttributes = " defaultProvider,hashAlgorithmType " >

4.关于配置文件的读写,首先对配置文件进行读取,我们都知道配置文件是一个XML文件,我们可以采用读取XML方式进行读写操作,但是,在ASP.NET中,MS提供了一套强类型的API对配置文件进行读写操作,如:

  
    
// 读取操作:
using System.Web.Configuration;
using System.Configuration;
namespace ConfingDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
MembershipSection ms
=
(MembershipSection)
WebConfigurationManager.GetSection(
" system.web/membership " ,
" ~/web.config " );
Response.Write(
" The default provider as set in config is: "
+ ms.DefaultProvider + " <br/> " );
}
}
}

// 写入操作:
using System.Web.Configuration;
using System.Configuration;
namespace ConfingDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Configuration config
=
WebConfigurationManager.OpenWebConfiguration( " ~ " );
MembershipSection ms
=
(MembershipSection)config.GetSection( " system.web/membership " );
ms.DefaultProvider
= " someOtherProvider " ;
config.Save();
}
}
}

若有疑问或不正之处,欢迎提出指正和讨论。

你可能感兴趣的:(asp.net)