Exam4_166.aspx:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = "该站点已经被访问<b>" & Application("guestcounter") & "</b>次"
Label2.Text = "当前在线<b>" & Application("currentguest") & "</b>人"
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>全站点计数器</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<div style="text-align: center">
<strong>站点计数器</strong><br />
<hr />
</div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</form>
</body>
</html>
Global.asax:
<%@ Application Language="VB" %>
<%@ Import Namespace="system.io" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' 在应用程序启动时运行的代码
application("guestcounter")=0
application("currentguest")=0
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' 在应用程序关闭时运行的代码
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' 在出现未处理的错误时运行的代码
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' 在新会话启动时运行的代码
dim i as integer
dim datastr,count as string
dim stream as filestream
dim strreadobj as streamreader
dim strwriterobj as streamwriter
stream=new filestream("counter.cnt",filemode.openorcreate,fileaccess.read)
strreadobj=new streamreader(stream)
datastr=strreadobj.readline
strreadobj.close()
count=cint(datastr)+1
Application("guestcounter") = count
stream=new filestream("counter.cnt",filemode.open,fileaccess.write)
strwriterobj=new streamwriter(stream)
strwriterobj.writeline(count)
strwriterobj.close()
Application("currentguest") += 1
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' 在会话结束时运行的代码。
' 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
' InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
' 或 SQLServer,则不会引发该事件。
Application("currentguest") -= 1
End Sub
</script>
结果:
提示错误:
错误 1 D:/Works/ASP.NET/ASP.NET 2.0上机练习与提高/Chater04 基本内置对象训练/Global.asax: ASP.NET 运行时错误: 没有为扩展名“.asax”注册的生成提供程序。可以在 machine.config 或 web.config 中的 <compilation><buildProviders> 节注册一个。请确保所注册的提供程序具有包含值“Web”或“All”的 BuildProviderAppliesToAttribute 属性。
网上有的说要修改一个文件(Web.config),但是我改了之后还是不行.