Run code automatically in ASP.NET Web Pages

Artikeln på svenska:

http://www.aspsidan.se/default.asp?page=readArticle&artId=711

When developing web sites you often need to run some code the first time the web site is loaded, or every time a page is loaded. In ASP.NET MVC you can do this in global.asax in Application_Start, or with custom action filters for a controller or action method.

Run code when the site starts

Since we don´t have a global.asax file or any controllers in our ASP.NET Web Pages projects, where we could run the code, we have new functionality.

The first time a visitor loads the site for the first time we might want to set a date in a global variable. After that we want to get the value from which page we want in the site.

To make it possible, we can create a file called _start.cshtml. As I mentioned in my article “Introduction to ASP.NET Web Pages”, we can not visit pages with a underscore as prefix, which means we can´t open this page directly. Instead it is executed the first time the web site loads.

In this file we remove all HTML and adds this:

@{
    AppData["timeStarted"] = DateTime.Now.ToLongTimeString();
}

AppData is a global variable which contains values for our web site. After that we create a new file called default.cshtml and add this code to it:

The site started at @ApplicationInstance.Application["timeStarted"]

When we visit default.cshtml we can se that we have got the current time. If we update the page, the time will be exactly the same. If we go to WebMatrix and click on the Restart button in the Ribbon and reloads the page, the time is updated, since the application is restarted.

Run code for every page

Except from running code when the site is started, we might want to run code before and after a page is loaded, for example logging.

To run code before and after a page is loaded, we create a new file called _init.cshtml. If we add code to this it is automatically executed before the page is loaded.

Add this to the file:

<p>This is loaded before the page! </p>

When we open a web page, this will automatically be added to the top of the page. We don´t need to have HTML here, but can instead use C# with the Razor syntax.

After _init.cshtml is loaded, the page loads itself automatically. We can choose when to load the page ourselves, though. To do that we just add a call to the method RunPage() in _init.cshtml. If we add code after calling RunPage(), it will be executed after the page run.

An example where we set LayoutPage in _init.cshtml:

<p>This is loaded before the page!</p>
@{
    LayoutPage = "~/_Layout.cshtml";
    RunPage();
}
<p>This is loaded after the page!</p>

Default.cshtml can then look like this:

The site started at @ApplicationInstance.Application["timeStarted"]

And _Layout.cshtml:

<!DOCTYPE html>
<html>
    <head>
        <title>Layout</title>
    </head>
    <body>
        @RenderBody()
    </body>
</html>

The layout page will now be automatically used by all cshtml pages, which makes it possible to have very clean pages, but still have a LayoutPage set.

The article as pdf and xps can be found here.

No Comments