Umbraco using Global.asax to handle events before the Request Handler.

Sometimes you want to perform session checks, redirect or anything else that would probably be best suited to running before the requested page is rendered. That's a function of the Global.asax, it allows you to hook into the request pipeline of the application and perform a function.

To do this, you need to change the code within the Global.asax file so that we can reference a custom class with our extra code, we need to change it to the following:

<%@ Application Inherits="MyWebsite.Global" Language="C#" %>

Once this is done, you'll need somewhere for your new Global class, create a new cs file in the app_code folder or another folder if you compile your website into a dll, then add the following:

using System;
using System.Web;
using System.Web.SessionState;
using Umbraco.Web;

namespace MyWebsite
    /// <summary>
    /// Global ASAX override.
    /// </summary>
    public class Global : UmbracoApplication
        // Init. Set up handlers here.
        public override void Init()
            HttpApplication objApplication = this as HttpApplication;

            objApplication.PreRequestHandlerExecute += PreRequestHandlerExecute;


        // Called when a session starts.
        private new void PreRequestHandlerExecute(object sender, EventArgs e)
            // Get current session.
            HttpSessionState objSession = ((UmbracoApplication)sender).Context.Session;

            // Make sure that there is an active session.
            if (objSession != null)
                // Work with the session here.

This is essentially the framework for our new Global.asax, we've already told Umbraco to use the new class so when you load your website now, all code within the 'PreRequestHandlerExecute' function is run for every requested page within the website. An example of why this might be useful is a security redirect if a particular session state has not been met, or an automatic login given the correct querystring value.

It's really that simple, you can add over event handlers into the class by checking out what intellisence has to offer on the objApplication object.

Published at

Tags: Umbraco,C#

Luke Alderton


Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Latest Comments
By Luke on How to add a Xamarin Forms Loading Screen/Overlay
23 Mar 2018, 15:03 PM
By Srusti Thakkar on How to add a Xamarin Forms Loading Screen/Overlay
23 Mar 2018, 13:16 PM
By Marco Teodoro on Custom section tree nodes and routes - Umbraco
21 Mar 2018, 11:08 AM
By ttt asd on Limit Google Autocomplete results to country and city
20 Mar 2018, 11:53 AM
By hamid on Limit Google Autocomplete results to country and city
19 Mar 2018, 21:13 PM
By matt on Auto/Custom height on Xamarin Forms WebView for Android and iOS
17 Mar 2018, 02:18 AM
By Mark Bell on JQuery Unobtrusive Validation for dynamically created elements
13 Mar 2018, 12:04 PM
By dsad on Simple fix for 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type xx'
9 Mar 2018, 09:46 AM
App Development
Game Development
Web Development