In this tutorial, you will learn how the Global.asax is constructed within a Umbraco project. If you have installed Umbraco via Nuget, it is likely that the sites global.asax might not be set up correctly. You can tell when this happens as no matter what you try your custom code within global.asax is never triggered. If you find yourself in this dilemma, read on to get unstuck 🔥🔥🔥,

First, open the global.asax file (not the code behind) and you should see this code:

This is telling IIS to use code found in Umbraco.Web.UmbracoApplication. This code contains everything Umbraco requires to bootstrap the CMS. it contains all the code, the logic, the rules and the config that makes Umbraco work. It is also possible to run your own custom logic when your application starts. The important part is that you also need to make sure this Umbraco code is run as well. To run some custom code, you need to change this line to point to your local global.asax.cs file:

Note, how the Codebehind property has been added. Ensure this value points to the physical location of the Global.asax.cs within your webroot. In the Inherits section, add your sites default namespace, followed by a . and then the application name. In this example, I called my website SampleSite so my global.ascx should look like this:

Note how the file inherits from Umbraco.Web.UmbracoApplication. This is key, otherwise, the CMS will not load. If everything has worked, you should now be able to add custom code in this code-behind 😊

How To Hook Into The Umbraco Event

Hooking into the application events within Umbraco is different compared to normal MVC. Below shows how a typical global.ascx within a vanilla MVC application could look:

When building an Umbraco site, Application_Start will not work. If you want some custom code to be hit when the website launches, you need to override, OnApplicationStarted() instead.

Also, some configurations that you can use in normal .NET will not work in Umbraco. RouteConfig.RegisterRoutes(RouteTable.Routes); is a good example. Routing in a Umbraco website differs greatly compared to a normal vanilla MVC website. In a normal website build, when you browse a site in your web browser, the segments in the Url used will be to map a request to a controller and an action. In Umbraco requests are mapped based on virtual pages within the CMS and the document type used to generate them. If a visitor requested this page www.website.com/landing, the pipeline won't simply try to map the request to a controller called LandingController. Instead, it will map to some Umbraco page called landing which has been created under the home page. The document type the content editor used to create that page will determine which controller/view Umbraco will attempt to load. Umbraco adds in its own handlers and routing rules to make this magic happen. By default a vanilla global.ascxwill have this rule:

If you accidentally copied this line into your project so it was run before the Umbraco routing rules, this line would break your site. This line is telling the site to ignore Umbraco routing and use default MVC routing instead. This can also be a reason why your custom controller is not being triggered in Umbraco 😊 Check yours to make sure you didn't accidentally copy this in!

Now we know about the differences between a standard MVC global.ascx and an Umbraco global,ascx, a working global,ascx.cs should look something like this:

You should not have a working global.asax. Hook into the pipeline however you see fit. Happy Coding 🤘