Skip to main content
Logo

Routing in Contensis

Routing in Contensis is implemented using a combination of standard .NET routing classes and methods and the Contensis APIs.

This, and the preceding tutorials will focus on implementing routing for a Contensis published site running on .NET web forms.

Routing can also be configured for applications that aren't hosted with Zengenti but pull content from Contensis through it's Delivery and Management APIs. This could be anything from a marketing dashboard written in Python to a mobile application built using React Native.

Here you can see the code to initialise routing and where we recommend you place this code, Movies.RouteConfig.cs.

Route config file in folder tree in Contensis
using System.Web.Routing;
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(Movies.App.RouteConfig), "Configure")]

namespace Movies.App
{
    public static class RouteConfig
    {
        public static void Configure()
        {
            Routing.RegisterCustomRoutes(RouteTable.Routes);
        }
    }
}

Warning: Changes to your websites App_Code folder can have a serious impact on your website and lead to it becoming unavailable. We recommend thoroughly testing any changes on your preview site before approving them to go to your live environment.

Note: The WebActivator dll needs to be uploaded first. Otherwise the code will produce an error as you will be referencing a library that isn't part of the application.

Adding the WebActivator custom dll

Code to initialise routing and handle requests would normally go in the global.asax file for a .NET application. For a Contensis published site, we don't allow developers to edit this file. This is because it contains code that, if deleted, would result in your application being unavailable.

If you are cloud hosted with Zengenti and routing isn't available natively through Contensis, you can raise a support ticket and a member of the support team can upload the dll to the bin directory and add an FTP exception. This will prevent the dll from being deleted as part of any upgrades or deployments.

If you are hosted on premise then you will need to download the nuget package https://www.nuget.org/packages/WebActivatorEx and generate the dll through Visual Studio. Once you have the dll, you can then upload it via FTP to the bin directory of all the relevant front end sites and exclude the WebActivatorEx.dll from the FTP cleaner by adding a reference to it to Management Console > Project Overview > Settings > FtpCleaner_ExcludedFiles and adding WebActivatorEx.dll.

Adding routing code to the app_code folder

Once this is done, you can then start to implement routing code in the app_code folder.

This quote from Microsoft developer Network (MSDN) explains the concept of the App_Code folder:

The App_Code folder therefore works much like the Bin folder, except that you can store source code in it instead of compiled code. The App_Code folder and its special status in an ASP.NET Web application makes it possible to create custom classes and other source-code-only files and use them in your Web application without having to compile them independently. MSDN

We recommend all code that defines custom routes and any associate methods and functions go in app_code so they be easily worked on and reused application wide.

Now we'll break down the code in more detail.

This line of code references the Web activator dll and allow us to specify a method of an object/class to execute prior to any content being rendered:

[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(Movies.App.RouteConfig), "Configure")]

Movies.App.RouteConfig

Defines the namespace and class where the method we want executed exists

Configure

Is the name of the method within the class we want to execute.

This line of code:

Routing.RegisterCustomRoutes(RouteTable.Routes);

...relates to a class and method covered in the defining routes article, where we will define our custom routes and what happens when a URL matches one of these.

Here is the final code:

using System.Web.Routing;
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(Movies.App.RouteConfig), "Configure")]

namespace Movies.App
{
    public static class RouteConfig
    {
        public static void Configure()
        {
            Routing.RegisterCustomRoutes(RouteTable.Routes);
        }
    }
}