ASP.NET Web Pages – Special Files

ASP.NET Web Pages have some special files which can contain code to maintain the entire website. From the last tutorial, open up the NestedLayouts and you will see the files:

_AppStart – In root of your website, stores global variables (for your site) and generally contains email settings, database initialization, etc. If this file exists it will be always checked and executed before the site loads.

_PageStart – In root and can also be in folders. If it is in a folder it will apply settings to all files in that directory; for example it can hold the layout file for a certain folder and you do not need to specify it every time.

App_Offline.htm – In your root directory this will set your application offline, which is very useful for maintenance. It must be spelt correctly and have the htm extension, not cshtml or HTML, and it cannot contain Razor code.

In the products folder of your NestedLayout website, click on Default.cshtml and remove the code block. If you run the page it will have no styling, as we no longer have linked it to the layout page.In the root of products folder create a new file called _PageStart.cshtml,  remove the HTML but leave the Razor code block, and then copy:

@{
    Layout="~/Products/_ProductsLayout.cshtml";
}

Now when you run the default page it will have styling.  This saves you a lot of work, since you don’t need to go to every page and specify the layout page.

Create a new file App_Offline.htm, and in the body tag write a message. Then run the default.cshtml and you should get the App_Offline message. When this happens you cannot browse directories or anything else.

We will discuss the _AppStart.cshtml file in the Sending Email tutorial.