What is an ASAX file?
A file with .asax extension is a file used by ASP.NET applications that resides on the server side. It contains code for responding to application-level and session-level events raised by ASP.NET or by HTTP modules. This also includes handling certain events when the application launches or shuts down. ASAX files are optional and only a single ASAX file is added to web applications to handle the application-level events and errors at the global level. Unlike ASPX pages, ASAX files do not contain any code to implement the functionality of the application.
ASAX File Format
ASAX files are written in plain text file format and are human readable. The most commonly used ASAX file is Global.asax that resides in the root directory of an ASP.NET application. Web Servers are configured to reject any incoming calls to this file to forbid users from downloading or viewing the code of this file.
Global.ASAX - An Example of ASAX File Format
A single ASAX file consists of multiple sections that are written to handle the application-level events. These are as follow.
- Application Directives - Application directives are tags that are used to define optional application-specific settings to be used by the ASP.NET parser when processing the Global.asax file. These directives are located in the start of the Global.asax file and are defined as follow.
<%@ directive attribute=value [attribute=value … ]%>
- Code Declaration Blocks - Code-declaration blocks are used to define sections of server code that are embedded in ASP.NET application files within the <script> blocks marked with a runat=“server” attribute. The following example shows how you can define event-handling logic for the EnterBtn_Click event.
<html>
<script language="C#" runat="server">
void EnterBtn_Click(Object Src, EventArgs E) {
Message.Text = "Hi " + Name.Text + ", welcome to ASP.NET!";
}
</script>
<body>
<form runat="server">
Enter your name: <asp:textbox id="Name" runat=server/>
<asp:button text="Enter" Onclick="EnterBtn_Click" runat="server"/>
<p>
<asp:label id="Message" runat=server/>
</form>
</body>
</html>
- Code Render Blocks - These define the inline code or expressions that execute when the page is rendered. The two styles of code render blocks include inline code and inline expressions. The former is used to define self-contained lines or blocks of code, while the lateral is used as a shortcut for calling the Write method.
Key Characteristics of the ASAX File Format
| Characteristic | Description |
|---|---|
| Single Instance Per Application | Only one ASAX file (typically Global.asax) exists per ASP.NET application, residing in the application’s root directory. |
| Automatically Parsed | The ASP.NET runtime automatically detects and processes the ASAX file without requiring manual intervention or registration. |
| Inheritance-Based | The file’s code-behind class inherits from HttpApplication, providing access to the complete ASP.NET object model and event system. |
| Not Directly Requestable | Unlike .aspx files, users cannot directly request Global.asax via browser—attempts are blocked by ASP.NET for security. |
| Recompilation Triggers | Modifying the ASAX file triggers automatic application recompilation, resetting application state but ensuring changes take effect immediately. |
| Language Agnostic | Supports any .NET language (C#, VB.NET, F#) through the code-behind model, though directives within the file specify which language is used. |
FAQ
Q1: What is the main purpose of Global.asax?
A: It handles application-level events and manages global state for ASP.NET web applications.
Q2: Can I have multiple ASAX files in one application?
A: No, only one Global.asax file is allowed per ASP.NET application, located in the root directory.
Q3: How is Global.asax different from web.config?
A: Web.config handles static configuration settings, while Global.asax contains dynamic code that runs during application events.
Q4: Does ASP.NET Core use Global.asax files?
A: No, ASP.NET Core replaces Global.asax with the Startup.cs class and middleware pipeline configuration.
Q5: What happens when I modify a live Global.asax file?
A: The application automatically recompiles and restarts, resetting all active sessions and application state.