配置文件 web.config 的讨论

可以直接修改,修改后立即生效,无需重新编译,可以把数据连接对象直接加到web.config 中,从而方便修改,如果写在类中的话,

修改后需要重新编译

1、所有的配置都必须放在<configuration></configuration>之间,

2、<appsettings>  </appsettings>之间是用户自定义配置,一般用来设置一些常量;

<add></add>用来添加常量,key 是常量的名称,value 是常量的值,程序中可以用

System.Configuration.ConfigurationSettings.AppSettings["key"] 来引用

如下面一个数据库连接的例子----------
]<appSettings>
<add key="con" value="server=127.0.0.1;database=dat;sa=sa;pwd=sa"/>
 </appSettings>
在其它页面可以用
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
       con.Open();
       SqlCommand cmd = new SqlCommand("select * from me", con);
       this.GridView1.DataSource = cmd.ExecuteReader();
       this.GridView1.DataBind();

这样修改起来就很方便,不用每次修改后重新编译,效果和class是一样的。

3、<location></location> 是区域标记 path="a"表示下面的标记只对目录a起作用。

4、<system.web></system.web>是关于整个应用程序的配置
如 在里面设置缓冲

<pages buffer="true"/>//缓冲启用(数据全部处理好 之前先先把处理好了缓冲在服务器上,否则处理一点发送一点‘以16K为一单元’),默认情况下启用缓冲
下面这个配置文件出错时转到一个专门的出错页面,mode="RemoteOnly" 表示在服务器端可以看见详细信息,其他的转到一个专门的页面。

如果 mode=“on” 则服务器和客户段都指向GenericErrorPage.htm
 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>

<compilation debug="true">编译时的配置,debug="true"便于调试,但发布时要改成false

身份验证:  <authentication mode="Windows"/>默认基于windows验证,有4种 windows, forms,non,passport ,我们一般用基于forms(cookies 和session)的 
<authentication mode="Forms">
      <forms loginUrl="login.asp" name="boyang" protection="All"></forms> 
</authentication>
    <authorization>
      <deny users="?,a"/>不容许匿名和a
      <allow users ="b"/>b 可以
    </authentication>  


posted @ 2006-03-30 10:30  New Yang Bo Element  阅读(319)  评论(0编辑  收藏  举报