HOW TO MAKE YOUR OWN CUSTOM ROUTES IN UMBRACO (ATTRIBUTE AND CONVENTION-BASED ROUTING)

victorighalo
3 min readJun 15, 2018

--

Introduction

My aim of writing this is to make Umbraco developers more confortable working with Umbraco by exposing some fundamental features that enable developers build full featured software with Umbraco more than just a website or a simple blog. A basic idea about C#, ASP.NET MVC and Umbraco will make it easier to understand.

What you will learn

1. What is Umbraco?

2. Umbraco Lifecycle methods

3. Umbraco Application Events

4. How to hook into events

5. How to register a custom Route

6. How to create a custom Controller

7. How to route to Controller actions

What is Umbraco?

Umbraco is an open source Content Management System written by an awesome guy, Niels Hartvig . It is written in C# and based on the glorious ASP.NET Web Framework, developed in the year 2000 and released as Open Source in 2004. Think of it as Wordpress, Drupal or Joomla only it is made with ASP.NET.

Umbraco Lifecycle methods

  1. ApplicationInitialized

This method executes first and Umbraco highly discourages executing any logic or accessing any Umbraco services in this method because at this state the application is still booting up so some services may not be available for use.

2. ApplicationStarting

This method executes second and Umbraco highly discourages executing any logic or accessing any Umbraco services in this method.

3. ApplicationStarted

This is the most commonly used because at this stage Umbraco’s boot sequence is complete and services are available for use. This is where we would assign a custom route for your application.

Umbraco Application Events

Application events in Umbraco enables developers to hook into the application’s workflow at different states or lifecycles of the application, such as before and after the application starts. Within any of the lifecycle methods discussed above, we have access to other event objects, such as ContentService, MediaService, FileService, etc. Developers can write code using these objects.

Each of these objects contain several extension methods that can be hooked into. For example: ContentService contains: Saving, Saved, Publishing, Published, Deleting, Deleted, etc. However, we would focus on a different approach, we won’t use any of these methods now.

Register Events (How to hook into events)

To register events, Umbraco exposes the ApplicationEventHandler base class. Developers can extend this class from anywhere within their application in order to gain access to Umbraco application events methods and override them. With this class we can execute Code during Umbraco’s bootup process. More details are in the next section.

Register a custom Route

Now we will get to the codes. Follow the instructions below to register a custom route:

1. Create a class to register a custom route file, name it RegisterEvents or whatever name suits your application.

2. Add the code below in the file

3. Observe we have a custom namespace on line 3. We will create that next.

On line 12 is where we execute our custom code to register a custom route (the custom route is yet to be created)

Add a custom route file

We registered a custom route in the ApplicationStarted Method, so we need to create that now. Create a file named Routing (or any name you choose) and add this code in it. Don’t forget to change the namespace whatever suits your application.

Create a custom controller

Create a controller named BaseController (optional but must match the controller value on line 13 on the Routing file) and add the code below:

Conclusion

If you have followed through and got all steps right you should be able to run your application and route to name or profile and see text content returned.

You have successfully configured your application to navigate to custom routes using convention-based and attribute routing at same time.

I am eager to see how Umbraco developers maximize the Power of mixing a solid and friendly CMS and the ASP.NET MVC architecture.

Thanks for reading. If you liked it drop some claps. If you have questions drop a comment below and I’ll respond ASAP.

--

--

victorighalo

I am a passionate software developer with love for Javascript, C# and PHP. I love to share knowledge and experience. @victorighalo1