</compilation> <!-设置应用全局环境-> <!-文件、请求、返回以gb2312编码,以保证浏览器正确显示中文-> globalization fileencoding=gb2312"requestencoding="gb2312 responseencoding="gb23 12"1> <!-定义用户出错的处理-> <!-出错缺省显示 defaultredirect指定的页面,mode为on时,遵循 customerrors配置段 <!-mode为off时,忽略用户出错,mode为. remotely时,本地才显示真正的出错原因-> customerrors defaultredirect="An ErrorHasOccured aspx? ErrNum=-l"mode=remote> <-当出错码为500时,显示 redirect指定的页面-> <error statuscode=500"redirect="An Error HasOccured aspx? ErrNum=500"> customerrors> <!-指定目录 webapp的访问权限-> <location path="webapp"> <!-非授权用户不能进入 webapp目录-> <security> deny users="?" I> <authorization> /security> </location> 定义安全属性--> <security> <authorization> <!-角色为 Adminstrators和所有的用户访问其指定的资源-> <allow roles="Adminstrators <allow users="*/ authorization> </configuration> 427小结 Config. web是aspx区别于asp的一个方面,我们可以用这个文件配置我们的很多 信息
</compilation> <!--设置应用全局环境--> <!--文件、请求、返回以 gb2312 编码,以保证浏览器正确显示中文--> <globalization fileencoding="gb2312" requestencoding="gb2312" responseencoding="gb2312"/> <!--定义用户出错的处理--> <!--出错缺省显示 defaultredirect 指定的页面,mode 为 on 时,遵循 customerrors 配置段--> <!--mode 为 off 时,忽略用户出错,mode 为 remoteonly 时,本地才显示真正的出错原因--> <customerrors defaultredirect="AnErrorHasOccured.aspx?ErrNum=-1" mode="remote"> <!--当出错码为 500 时,显示 redirect 指定的页面--> <error statuscode="500" redirect="AnErrorHasOccured.aspx?ErrNum=500"/> </customerrors> <!--指定目录 webapp 的访问权限--> <location path="webapp” > <!--非授权用户不能进入 webapp 目录--> <security> <authorization> <deny users="?" /> </authorization> </security> </location> <!--定义安全属性--> <security> <authorization> <!--角色为 Adminstrators 和所有的用户访问其指定的资源--> <allow roles="Adminstrators"/> <allow users="*" /> </authorization> </security> </configuration> 4.2.7 小结 Config.web 是 aspx 区别于 asp 的一个方面,我们可以用这个文件配置我们的很多 信息
第三章编写 globa l.asax 为了编写用户界面的应用程序,开发者可以把应用程序标准的逻辑和时间处理的代码加 到 Web Application里面。这些代码不产生用户界面,也不想英单个得页面的请求。事实上 这些代码处理更高水平的事件,如 Application Start, Application_End Session_ Start, Session End等等。开发者通过放在web应用程序根目录下面的 Global asax来响应这些事件。 Asp. net通过一个动态的 NET Frame Work类自动解析和编译这个文件,这个类就是 HttpapplicatiOn基类,在第一时间里面,在这个文件里面的应用程序的资源将会被响应 首先,在包含有请求的应用程序名字空间中被访问之前, Global.asaⅸX将被解析和编译 成 NET Framework的一个类。这个文件本身有拒绝被访问的配置。 下面我们来看看这个文件里面的具体内容,首先我们声明这个文件的使用语言、运行环 境 <script language="VBrunat=server> 相关方法 </script> 然后我们就可以定义各种方法了, Sub Application Start 方法的属性等 End sub 如果事件处理代码需要用到名字空间,我们可以这样来引用它 <%(@ Import Namespace="System. Data. SQL%> 下面我们来看看这个文件的具体应用,首先我们在我们的 Web server上建立一个 Global asax文件,我们在里面加上我们的代码 <script language=VB"runat=server> “相关方法 Sub Application Start 方法的属性等 End Sub Sub application Start(Sender As Object, EAs EventArgs) Application. Locko Application("counter")=CType( Application("counter")+ l, Int32)
第三章 编写 global.asax 为了编写用户界面的应用程序,开发者可以把应用程序标准的逻辑和时间处理的代码加 到 Web Application 里面。这些代码不产生用户界面,也不想英单个得页面的请求。事实上, 这 些 代 码 处 理 更 高 水 平 的 事 件 , 如 Application_Start, Application_End, Session_Start, Session_End,等等。开发者通过放在 web 应用程序根目录下面的 Global.asax 来响应这些事件。 Asp.net 通过一个动态的.NET FrameWork 类自动解析和编译这个文件,这个类就是 HttpApplication 基类,在第一时间里面,在这个文件里面的应用程序的资源将会被响应。 首先,在包含有请求的应用程序名字空间中被访问之前,Global.asax 将被解析和编译 成.NET Framework 的一个类。这个文件本身有拒绝被访问的配置。 下面我们来看看这个文件里面的具体内容,首先我们声明这个文件的使用语言、运行环 境: <script language=”VB” runat=server> ‘相关方法 </script> 然后我们就可以定义各种方法了, Sub Application_Start() ‘方法的属性等 End Sub 如果事件处理代码需要用到名字空间,我们可以这样来引用它: <%@ Import Namespace=”System.Data.SQL”%> 下面我们来看看这个文件的具体应用,首先我们在我们的 Web Server 上建立一个 Global.asax 文件,我们在里面加上我们的代码: <script language=”VB” runat=server> ‘相关方法 Sub Application_Start() ‘方法的属性等 End Sub Sub Application_Start(Sender As Object, E As EventArgs) Application.Lock() Application("counter") = CType(Application("counter") + 1, Int32)
Application. UnLock End Su Sub application End( Sender As Object, E As Event Args) Clean up application resources here End Sub Sub Session Start( Sender As Object, E As EventArgs) Response. Write(" 'Session正在启动<br>") End sub Sub Session End( Sender As Object, E As Event Args Clean up session resources here End Sub 当然,我们还要配置 Config. web,用来指定岀错信息的打印页面。根据上面我们配置 Config. web的经验,我们很容易的就可以对这个文件进行配置: <customerrors modes"on"defaultredirect=error. htm">> globalization requestencoding="gb23 12 responseencoding="gb23 12"/> </configuration> 第二句话就是配置我们指定的出错页面语句。我们写两个页面来实现它,一个为出错页面, 一个为实现这个功能的aspx页面。出错页面很简单的,就是报告程序出错时显示的信息 我们就写“在 config web里面配置的连接!“,是经过aspx页面甩出来的。 在aspx页面,我们用下面的语句来响应出错按钮点击事件 Sub error Click(Sender As object, E As EventArgs) 甩出异常! throw New Exception) nd Sub 以外我们的响应 Session的方法用下面的语句来说明 Sub Session Click(Sender As Object, EAs EventArgs) Session. abandono Response. Redirect("global. aspx") End sub 下面是完整的代码 <html>
Application.UnLock() End Sub Sub Application_End(Sender As Object, E As EventArgs) ' Clean up application resources here End Sub Sub Session_Start(Sender As Object, E As EventArgs) Response.Write("Session 正在启动...<br>") End Sub Sub Session_End(Sender As Object, E As EventArgs) ' Clean up session resources here End Sub </script> 当然,我们还要配置 Config.web,用来指定出错信息的打印页面。根据上面我们配置 Config.web 的经验,我们很容易的就可以对这个文件进行配置: <configuration> <customerrors mode="on" defaultredirect="error.htm" /> <globalization requestencoding="gb2312" responseencoding="gb2312" /> </configuration> 第二句话就是配置我们指定的出错页面语句。我们写两个页面来实现它,一个为出错页面, 一个为实现这个功能的 aspx 页面。出错页面很简单的,就是报告程序出错时显示的信息, 我们就写“在 config.web 里面配置的连接!“,是经过 aspx 页面甩出来的。 在 aspx 页面,我们用下面的语句来响应出错按钮点击事件: Sub Error_Click(Sender As Object, E As EventArgs) ‘甩出异常! throw New Exception() End Sub 以外我们的响应 Session 的方法用下面的语句来说明: Sub Session_Click(Sender As Object, E As EventArgs) Session.Abandon() Response.Redirect("global.aspx") End Sub 下面是完整的代码: <html>
<script language="VB"runat="server"> 页面导入 Sub Page Load( Sender As Object, EAs EventArgs) Response. Write("正在装入页面 End Sub Session事件 Sub ssaidy(Sender As Object, E As EventArgs) Session. abandono esponse Redirect("global. aspx") End Sub 响应错误方法 Sub esaidy(sender As object, EAs EventArgs) 抛出异常 throw New Exception End sub <body> <br><br><br> <form runat=server"> < cinput type=" 'submit" value="刷新这个页面" runat=" server" < Input type=" submit" On Server Click=" sandy"Ⅷalue="结束这个 Session runat="server"I> < cinput type=" submit" On Server Click=" sandy"alue="错误表示" runat=" server">Φp br><br> <htmI> 运行结果如下
<script language="VB" runat="server"> '页面导入 Sub Page_Load(Sender As Object, E As EventArgs) Response.Write("正在装入页面...<br>") End Sub 'Session 事件 Sub ssaidy(Sender As Object, E As EventArgs) Session.Abandon() Response.Redirect("global.aspx") End Sub '响应错误方法 Sub esaidy(Sender As Object, E As EventArgs) '抛出异常 throw New Exception() End Sub </script> <body> <br><br><br> <center> <form runat="server"> <input type="submit" Value="刷新这个页面" runat="server"/> <input type="submit" OnServerClick="ssaidy" Value=" 结 束 这 个 Session" runat="server"/> <input type="submit" OnServerClick="esaidy" Value="错误表示" runat="server"/><p> <hr> </form> </center> <br><br> </body> </html> 运行结果如下:
ahttp://ocalhost/quickstart/aspplus/samples/apps/vb/application1/applicationl.as..-ox Ele Edt view Favorites Tools Help 中Ba+③的备seah图 Favorites History昌目 NET-> Global asax文件 请求开始 正在装入页面 刷新这个页面」结束这个 "Session错误表示」 请求结束 aDone 厂 Local intranet 点击“错误表示“按钮,显示如下: ahttp://localhost/quickstart/aspplus/samples/apps/vb/applicationl/errorpage.htm.-ox dit yew Favorites Tools Help 中B3k、→③备 Search FAVorites Hstory昌图了目 在 config.Web里面配置的连接! clOne Local intranet 431小结 讲述了配置文件 Global.asax的配置问题, Global asax文件对一个NET技术构建的 WEB站点来讲,是非常必须的,本章我们的内容就是针对它讲的
点击“错误表示“按钮,显示如下: 4.3.1 小结 讲述了配置文件 Global.asax 的配置问题,Global.asax 文件对一个.NET 技术构建的 WEB 站点来讲,是非常必须的,本章我们的内容就是针对它讲的