190 likes | 326 Views
第 9 讲 ASP.NET 应用程序. Web 数据库 设计与应用. 1 ASP.NET 应用程序介绍 2 配置文件 3 数据保存 4 跟踪 5 部署. VB.NET. ADO.NET. Use Visual Studio .NET. ASP.NET. Visual Studio.NET. 9.1 ASP.NET 应用程序介绍. 应用程序文件 Global.asax. Global.asax 文件驻留在应用程序的根目录中 ASP.NET Global.asax 文件可以和 ASP Global.asa 文件共存
E N D
第9讲 ASP.NET应用程序 Web数据库 设计与应用 • 1 ASP.NET应用程序介绍 • 2 配置文件 • 3 数据保存 • 4 跟踪 • 5 部署 VB.NET ADO.NET Use Visual Studio .NET ASP.NET Visual Studio.NET
应用程序文件Global.asax • Global.asax 文件驻留在应用程序的根目录中 • ASP.NET Global.asax 文件可以和 ASP Global.asa 文件共存 • Global.asax 文件是可选的 • Global.asax 的修改
9.2 配置文件 • Machine.config 文件 • Machine-level settings • Web.config 文件 • Application and directory-level settings • Both Machine.config and Web.config files are: • Well-formed XML • Camel格式 camelCase • 可扩展Extendable
Machine.config • Machine.config的配置影响所有 Web 应用 • 一个Web服务器上只有一个Machine.config文件 • 大多数配置可以被 Web.config覆盖 • 位置 • %SystemRoot%\Microsoft.NET\Framework\V版本号\CONFIG\Machine.config
Web.config • 每个Web应用程序可以包含 Web.config files • 根目录下的Web.config其配置设置将应用于该站点的所有应用程序 • 子目录下的Web.config其配置设置只使用于该子目录
配置文件的大小写 • 配置文件中的标记是区分大小写的。 • 标记名和属性名是 Camel 大小写形式的 • 第一个字符是小写的,任何后面连接单词的第一个字母是大写的。例如:appSettings不能是AppSettings。 • 属性值是 Pascal 大小写形式的 • 第一个字符大写的,任何后面连接单词的第一个字母也是大写的。
CONFIG Machine.config VirtualDir Web.config SubDir Web.config 配置文件的继承 • Web.config 从 Machine.config继承 • Web.config的配置覆盖machine.config • 子目录下的 Web.config继承根目录Web.config的配置,有冲突时可以覆盖
9.3应用程序数据保存技术 状态管理 • 使用ASP.NET Cache(缓存) • 使用Web.config 变量 • 使用Session 和Application变量 • 使用视图状态(ViewState) • 使用客户端 Cookie
使用ASP.NET Cache(缓存) • ASP.NET Cache: • 存储对象和值,应用程序可以重复使用 • 存储对象 • 读取对象 Cache.Insert("mykey", myValue, _ Nothing, DateTime.Now.AddHours(1), _ TimeSpan.Zero) myValue = Cache("mykey") If myValue <> Nothing Then DisplayData(myValue) End If
使用Web.config 变量 • 使用 Web.config的appSettings • 获取appSettings的值 <configuration> <appSettings> <add key=“conn_pubs" value="server=localhost;uid=sa;pwd=;database=pubs“ /> </appSettings> </configuration> Dim strConn As String = ConfigurationSettings.AppSettings("conn_pubs")
使用Session 和Application变量 • Session对象为某一特定用户存储信息 • Application对象在应用程序所有用户间共享信息 Sub Session_Start(ByVal Sender As Object, _ ByVal e As EventArgs) Session("BackColor") = "beige" Session("ForeColor") = "black" End Sub Sub Application_Start(ByVal Sender As Object, _ ByVal e As EventArgs) Application("NumberofVisitors") = 0 End Sub
使用视图状态(ViewState) • 保存对象或变量 • 获取 ViewState("color") = "yellow" Dim strColor as String strColor = CStr(ViewState("color"))
创建客户端 Cookie • 创建Cookie If Request.Cookies("preferences1") Is Nothing Dim Ck As New HttpCookie("preferences1") Ck.Values.Add("ForeColor","black") '写入客户端文件cookie.Expires = DateTime.MaxValue Response.AppendCookie(Ck) End If • 注意: • 如果没有设置cookie.Expires属性,则不会在客户端保存cookie文件 • cookie文件的默认位置:\Documents and Settings\用户名\Cookies
获取 Cookie 值 Dim ck As HttpCookie ck = Request.Cookies("preferences1") Dim fc as String = Ck.Values("ForeColor")
页跟踪 • 启用页跟踪 • 指定顺序 • 插入Trace消息 • Trace.IsEnabled <%@ Page Language="VB" Trace="true"%> <%@ Page Language="VB" Trace="true" TraceMode="SortByCategory"%> Trace.Write("category", "message") If Trace.IsEnabled Trace.Write(“显示消息") End If
启用应用程序级跟踪 • 在根目录 Web.config 文件中启用整个应用的跟踪 <trace enabled="true" traceMode="SortByCategory" requestLimit="40" pageOutput="true"/>