Tech Tip: Enabling Attribute Routing in Umbraco 8

Updated on: 01 Jun 2020

Umbraco 8 now makes use of Components and Composers in a process called 'Composing' to customise the behavior of Umbraco at start up.

To learn about Components and Composers in Umbraco 8's Composing, please refer to https://our.umbraco.com/documentation/implementation/composing/ .

To enable attribute routing in Umbraco 8, we will need to go through the 'Composing' process.

When enabling attribute routing, there are two types of attribute routing we can enable:

  1. MVC Attribute Routing
    • For controllers which inherit from the standard MVC Controller such as Controller,UmbracoBackofficeController, SurfaceController etc.
  2. Web API Attribute Routing
    • For controllers which inherit from the Web API Controller such as ApiController, UmbracoApiController etc

Enabling MVC Attribute Routing in Umbraco 8

The first thing we need to do is create a Component for enabling MVC Attribute Routing. To do so we will need to create a new class which inherits from Umbraco's Component class. I usually put my Component classes in a Components folder in the Umbraco project.

The component class code should look something like below:

using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Composing;

namespace MinatCoding.CMS.Components
{
    public class MvcRoutingComponent : IComponent
    {
        public void Initialize()
        {           
            RouteTable.Routes.MapMvcAttributeRoutes();
        }

        public void Terminate()
        {

        }
    }
}
MvcRoutingComponent.cs

The code in the component class which enables the attribute routing is the code below:

RouteTable.Routes.MapMvcAttributeRoutes();

Enabling API Attribute Routing in Umbraco 8

To enable API attribute Routing, we will first need to create a web api config class where global configuration for the Web Api framework is handled. The code for that class can be taken below:

using System.Web.Http;

namespace MinatCoding.CMS.App_Config
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
        }
    }
}
WebApiConfig.cs

Next is to create a Umbraco component that will run the above class. The component will look something like below:

using Umbraco.Core.Composing;

namespace MinatCoding.CMS.Components
{
    public class WebApiRoutingComponent : IComponent
    {
        public void Initialize()
        {           
            System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
        }

        public void Terminate()
        {

        }
    }
}
WebApiRoutingComponent.cs

The key code for executing the web api config in the component is:

System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);

Registering the Attribute Routing Component

Next is to register the components in a composer class. The code do so should look similar to the code shown below:

using MinatCoding.CMS.Components;
using Umbraco.Core;
using Umbraco.Core.Composing;

namespace GrowthOps.Core.CMS.Composers
{
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class AppComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<MvcRoutingComponent>();
            composition.Components().Append<WebApiRoutingComponent>();
        }
    }
}
RoutingComposer.cs

Once the Component and Composer classes above are set up in your Umbraco project , you can start using attribute routing as how you normally would in a .Net MVC or Web API project.

Happy coding!

Here are some more posts