[Mono-list] Newbie Question: Web app life cycle events
Roger
roger.varley at googlemail.com
Mon Oct 27 06:05:15 EDT 2008
Hi
I'm new to mono and C#, I'm coming from a Java background. I've built my
first "test" web-app using monodevelop using a tutorial I found on the web.
(Code attached below). Everything works as expected.
I then tried playing around with the Global.asax.cs by inserting
Console.WriteLine() into each of the calls to try and get a feel for the
life_cycle events and when they're called. Now when I run my application I
get one call to Application_Start() - which I would've expected, but I get
multiple calls to Application_BeginRequest, Session_Start() and
Application_EndRequest() - which I would not have expected. Why do I get
three calls?
Regards
**** Default.aspx ****
<%@ Page Language="C#" Inherits="MyWebProject.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Default</title>
</head>
<body>
<form id="form1" runat="server">
<asp:button id="clickMeButton" runat="server" text="Click Me"
onClick="clickMeButton_Click"/>
<asp:label id="outputLabel" runat="server"/>
</form>
</body>
</html>
**** Default.aspx.cs ****
using System;
using System.Web;
using System.Web.UI;
namespace MyWebProject
{
public partial class Default : System.Web.UI.Page
{
public void clickMeButton_Click(object sender, EventArgs e) {
object val = ViewState["ButtonClickCount"];
int i = (val == null)? 1 : (int)val + 1;
outputLabel.Text = string.Format ("You clicked me {0} {1}", i,
i==1?"time":"times");
ViewState["ButtonClickCount"] = i;
}
}
}
**** Global.asax.cs ****
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
namespace MyWebProject
{
public class Global : System.Web.HttpApplication
{
protected virtual void Application_Start(object sender, EventArgs e)
{
Console.WriteLine("Application_Start is called");
}
protected virtual void Session_Start(object sender, EventArgs e)
{
Console.WriteLine("Session_Start is called");
}
protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
Console.WriteLine("Application_BeginRequest is called");
}
protected virtual void Application_EndRequest(object sender, EventArgs e)
{
Console.WriteLine("Application_EndRequest is called");
}
protected virtual void Application_AuthenticateRequest(object sender,
EventArgs e)
{
}
protected virtual void Application_Error(object sender, EventArgs e)
{
}
protected virtual void Session_End(object sender, EventArgs e)
{
Console.WriteLine("Session_End is called");
}
protected virtual void Application_End(object sender, EventArgs e)
{
Console.WriteLine("Application_End is called");
}
}
}
More information about the Mono-list
mailing list