How To Enable The Global.asax in Umbraco

If you have installed Umbraco via Nuget, it is likely that the sites global.asax might not be set-up correctly.  If you want to trigger some custom code on application start-up and your global.ascx is never being hit, then read on!  In today’s guide, I’m going to cover how to enable it in a few simple steps.

In case you don’t know how to install Umbraco, then I suggest you read this article, Installing Umbraco 7 In Visual Studio Via Nuget

Default Implementation

If you open the global.ascx file (not the code behind), you should see this:

<%@ Application Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>

This is telling IIS to bypass your global.ascx.cs file(the code-behind) and use the Umbraco Global.ascx code behind, that is located within UmbracoApplication.  If we want to add our own custom logic into the start-up, then we need it to point to the file in the webroot.  You can do this by changing the following properties:

<%@ Application Codebehind="Global.asax.cs" Inherits="SampleSite.MvcApplication" Language="C#" %>          

Add the Codebehind property and point it to the physical path to Global.asax.cs.  In the Inherits section, use your site’s name and namespace to point to the file.  I called my website ‘SampleSite’ so my global.ascx should look like this:

namespace SampleSite
{
public class MvcApplication : Umbraco.Web.UmbracoApplication
{
}
}

It’s Not Working The Same As I’m Used To!

On a standard MVC web project the default global.ascx might look something similar to this:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}

The first thing we should cover; Umbraco has different overload.  When you want the code to be hit on website launch, you need to override, OnApplicationStarted().

The second thing you need to understand is to remove this line ‘ RouteConfig.RegisterRoutes(RouteTable.Routes);’, why?  Routing in an MVC application like Umbraco differs greatly than a normal website.

In a normal website build, when you browse a site in your web browser, the Url used will be used to call a matching controller.  If you have a LandingController and type in www.website.com/landing that controller will be hit, simple.

In Umbraco we deal with virtual pages, the Url www.website.com/landing won’t map to a direct controller, instead it will map to some document type with a matching name within in the Umbraco database.

To ensure the page gets rendered, Umbraco overrides the Global.ascx, adds in its own handlers to find the matching Umbraco document type and then returns that result to a controller.

In Umbraco controller match document type names, not Url paths. By default your global.ascx will have this line included:

RouteConfig.RegisterRoutes(RouteTable.Routes);

This line is telling the site to ignore Umbraco routing and use default MVC routing.  (If you find that your Umbraco controllers are never being hit after making the change above, check this 🙂

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

namespace SampleSite
{
public class MvcApplication : Umbraco.Web.UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
RegisterGlobalFilters(GlobalFilters.Filters);
}

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new LayoutViewModelResultFilter());
}
}
}

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] To prevent Umbraco blowing up, you will need to hook into the web.configs Application_Error() method. If you haven’t hooked up your Global.ascx file yet to work with custom code, I would recommend you read How To Enable The Global.asax in Umbraco […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *