ASP.NET Web Pages Using The Razor Syntax
Chapter 18 – Customizing Site-Wide Behavior
Enter the words for the test. If you pass the reCAPTCHA test, you see a message to that effect;
otherwise you see an error message and the reCAPTCHA control is redisplayed.
Running Code Before and After Files in a Folder
Just like you can use _AppStart.cshtml to write code before pages in the site run, you can write code
that runs before (and after) any page in a particular folder run. This is useful for things like setting the
same layout page for all the pages in a folder, or for checking that a user is logged in before running a
page in the folder.
For pages in particular folders, you can create code in a file named _PageStart.cshtml. The following
diagram shows how the _PageStart.cshtml page works. When a request comes in for a page, ASP.NET
first checks for a _AppStart.cshtml page and runs that. Then ASP.NET checks whether there's an
_PageStart.cshtml page, and if so, runs that. It then runs the requested page.
Inside the _PageStart.cshtml page, you can specify where during processing you want the requested
page to run by including a
method. This lets you run code before the requested page runs and
then again after it. If you don't include
, all the code in _PageStart.cshtml runs, and then the
requested page runs automatically.
ASP.NET lets you create a hierarchy of _PageStart.cshtml files. You can put an _PageStart.cshtml file in
the root of the site and in any subfolder. When a page is requested, the _PageStart.cshtml file at the
top-most level (nearest to the site root) runs, followed by the _PageStart.cshtml file in the next
subfolder, and so on down the subfolder structure until the request reaches the folder that contains the
requested page. After all the applicable _PageStart.cshtml files have run, the requested page runs.
For example, you might have the following combination of _PageStart.cshtml files and default.cshtml
@* ~/_PageStart.cshtml *@