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;

            base.Init();
        }

        // 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

Comments

Janine Gardiola
How to execute a code only once when the app has started? Like on a regular application_start() mvc
26/04/2018
Luke
Put your code within the Init() method. :)
27/04/2018
Share with
Tags
Latest Comments
By Mark Gentry on Windows Server 2019 - Change product key does nothing
20 Aug 2021, 03:30 AM
By Cathy on In-Place Upgrade for a Windows Server domain controller
31 Jul 2021, 18:28 PM
By Mr. Greymatter on Raspberry Pi - Running Java app on Raspbian
16 Feb 2021, 07:35 AM
By Mikko Seittenranta on Xamarin Forms multiple instances of same app open
16 Feb 2021, 04:34 AM
By Andrew on Auto/Custom height on Xamarin Forms WebView for Android and iOS
22 Jan 2021, 22:15 PM
By Nick on Raspberry Pi - Running Java app on Raspbian
14 Oct 2020, 19:37 PM
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development