Easy Tutorial
❮ Control Hyperlink Webpages Webgrid ❯

ASP.NET Web Pages - Global Pages


This chapter introduces the global pages AppStart and PageStart.


Before Web Startup: _AppStart

Most server-side code is written within individual web pages. For example, if a page contains an input form, that page typically includes server-side code to read the form data.

However, you can create a page named _AppStart in the root directory of your site to execute code before the site starts. If this page exists, ASP.NET will run it before any other pages in the site are requested.

Typical uses for _AppStart include startup code and initializing global values (such as counters and global names).

Note 1: The file extension of _AppStart matches that of your web pages, for example: _AppStart.cshtml.

Note 2: _AppStart has an underscore prefix. Therefore, these files cannot be browsed directly.


Before Each Page: _PageStart

Just as _AppStart runs before your site starts, you can write code that runs before any page in a folder.

For each folder in your website, you can add a file named _PageStart.

Typical uses for _PageStart include setting the layout page for all pages in a folder, or checking if a user is logged in before running a page.


How It Works

The following diagram shows how it works:

When a request is received, ASP.NET first checks if _AppStart exists. If _AppStart exists and it is the first request received by the site, _AppStart is run.

Then ASP.NET checks if _PageStart exists. If _PageStart exists, it is run before any other requested pages.

You can call RunPage() in _PageStart to specify where the requested page should run. Otherwise, by default, the requested page runs after _PageStart.

❮ Control Hyperlink Webpages Webgrid ❯