Umbraco 8 Custom Routing

By SG Digital on 11/30/2019

Umbraco 8 handles custom routing slightly differently to Umbraco 7. If you are trying to use your own custom MVC controllers in Umbraco 8 then here is how you do it:

1. Create a folder in your web project called CustomRoutes.

2. Add a c# class called RegisterCustomRouteComponent.

3. Copy and paste the following code into your class:

using System;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Composing;
namespace Umbraco8.Components
{
/// <summary>
/// Register Custom Route Composer
/// Register any custom routes here - see https://our.umbraco.com/Documentation/Reference/Routing/custom-routes and https://our.umbraco.com/forum/umbraco-8/96167-registering-custom-routes-in-umbraco-8
/// </summary>
public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
{
}
/// <summary>
/// Register Custom Route Component
/// </summary>
public class RegisterCustomRouteComponent : IComponent
{
public void Initialize()
{
// Register Custom Forms Controller
RouteTable.Routes.MapRoute("Forms", "forms", new { controller = "Forms", action = "Index" });
// Register Custom ML Controller
RouteTable.Routes.MapRoute("AzureML", "azureml-garden-design", new { controller = "AzureML", action = "Index" });
}
/// <summary>
/// Terminate
/// </summary>
public void Terminate()
{
throw new NotImplementedException();
}
}
}

4. In the Initialize method, substitute my example custom routes for your own, mapping them onto actions on your own custom MVC controllers in Umbraco 8.

 

Job Done!

Share this article...

Join the Discussion...