关于配置文件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");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!