关于配置文件Web.config文件的家常事

1. 在Web.config文件中数据库连接字符串的运用

     a.将web.config文件中<system.web>标签之上的<connectionStrings />更改如下:

         <connectionStrings>

           <add  name="ConnStr"  connectionString="Data Source=.;Initial  Catalog=YGG;Integrated Security=True" />

         </connectionStrings>

     b.在后台防问时数据库连接串的写法

         string   str=ConfigurationManager.ConnectionStrings["ConnStr"].Tostring();

         也可如下:

         string  str=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

2.在Web.config文件中定义全局变量(有时会把一些常用的如公司名的变量放于此,改变时只改此处便可)

      a.将web.config文件中<system.web>标签之上的<appSettings />更改如下:

          <appSetting>

             <add  key="YGG"  value="123" />

          </appSetting>

      b.在后仿问(如果点不出来就添加.Net引用system.configuration 便可)

        string  str=ConfigurationManager.appSetting["YGG"].ToString();

3.关于网站发部不取用调试

        在<system.web>标签下有个<compilation debug="false"> 其debug为false时为方便调试

         因调试会产生额外数据,所以发部网站时希将<compilation debug="true">

4.出错页面配置************************************************

       <customErrors mode="on" defaultRedirect="A.aspx">

      </customErrors>

      如果要反回出错前的页面,可以在错误页A.aspx中添加一HyperLink控件Text=返回并后台代码如下

         protected void  Page_Load(object sender , EventArgs e )

            {

                  if(!Page.IsPostBack)

                      {

                         this.HyperLink1.NavigateUrl=Request.QueryString[0].ToString();

                      }

            }

5.页面权限,为了网站安全,实现必须登入方能仿问本站,否则将跳到登入页面。

      a 把<system.web>标签下的<authentcation ……/>作如下代码更改

             <authentcation  mode="Forms">

                <forms  name="cookieName"  loginUrl="登入页面.aspx">

                </forms>

             </authentcation>

      b. 再添加如下标签代码(说明:此标签须自行添加)

             <authorization>

                <deny  users="?"  />

             </authorization>

      c.登入页面后台匹配代码

             protected  void  button1_Click(object sender,EventArgs e)  //登入按钮事件

               {

                  if( Confirm(this.txtName.Text , this.txtPwd.Text) )

                     {

                        FormsAuthentication.RedirectLoginPage(this.txtName.Text,this.CheckBox.Checked);

                                                                                *保存cookie的信息    *是否永久保存            

                     }

                  else

                    {

                       Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script language=javascript>window.alert('用户名或密码错误!')</script>");

                    }

               } 

             public   bool   Confirm(  string userName , string  userPwd)

                {

                   //该方法用于用户验证,须连接数据库:此处略

                }

      D.安全退出按钮事件,此按钮在非登入页面

             protected  void   Buttons_Click( object   sender, EventArgs e)   //安全退出按钮事件

                {

                    FormsAuthentication.SignOut();

                    Response.Redirect("登入页面.aspx");

               }

posted @ 2012-04-25 21:55  酒沉吟  阅读(303)  评论(0编辑  收藏  举报