Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Web Pages Global Files

ASP.NET Web Pages - Global Pages


This chapter introduces the global pages AppStart and PageStart.


Before Web Start: _AppStart

Most server-side code is written inside individual web pages. For example, if a web page contains an input form, then that page usually contains the server-side code used to read the form data.

However, you can create a page named _AppStart in the root directory of your site so that code execution starts before the site starts. If this page exists, ASP.NET runs this page first when other pages in the site are requested.

A typical use of _AppStart is to start code and initialize global values (such as counters and global names).

Note 1:The file extension of _AppStart is the same as 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 each folder.

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

A typical use of _PageStart is to set the layout page for all pages in a folder, or to check whether the user is logged in before running a page.


How does it work?

The figure below shows how it works:

PageStart

When a request is received, ASP.NET first checks whether _AppStart exists. If _AppStart exists and this is the first request the site receives, _AppStart runs.

Then ASP.NET checks whether _PageStart exists. If _PageStart exists, it runs before other requested pages run.

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