Using ASP.NET Routing in mojoPortal

In version 2.4.0.4 of mojoPortal or higher you can plugin your own code to register MVC or Web API routes. You do this by creating a class that implements IRegisterRoutes, the interface is defined as follows:

using System.Web.Http;
using System.Web.Routing;
using System.Web.Mvc;

namespace mojoPortal.Web.Routing
{
    public interface IRegisterRoutes
    {
        void Register(HttpConfiguration config);
        void RegisterRoutes(RouteCollection routes);
        void RegisterGlobalFilters(GlobalFilterCollection filters);
    }
}

We do have a working example of an implementation of this interface that is used to register a Web API route used in the forums, the code for that is shown below:

using mojoPortal.Web.Routing;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace mojoPortal.Features
{
    public class RouteRegistrar : IRegisterRoutes
    {
        public void Register(HttpConfiguration config)
        {
            // api routes
            config.Routes.MapHttpRoute(
                name: "ForumMod",
                routeTemplate: "api/forummod/{id}",
                defaults: new { controller = "ForumMod", id = RouteParameter.Optional }
            );
        }

        public void RegisterRoutes(RouteCollection routes)
        {
            // mvc routes
        }

        public void RegisterGlobalFilters(GlobalFilterCollection filters)
        { }
    }
}

The above code is compiled into mojoPortal.Features.UI.dll and it is plugged in by a config file named FeaturesRoutes.config under /Setup/RouteRegistrars. The contents of the file is as follows:

<?xml version="1.0"?>
<Routes>
    <IRegisterRoutes type="mojoPortal.Features.RouteRegistrar, mojoPortal.Features.UI" />
</Routes>

You can implement your own class to register routes, compile it into a dll in the /bin folder and add a file to plug it in the same as we have done in the above example.

Legacy Information

The information below was correct for versions of mojoPortal older than 2.4.0.4, in that version we removed the code for the below implementation which only worked for MVC routes and implemented the new approach documented above which allows registering both MVC and Web API routes. Note however, that if someone still really needs the old way to work it is possible. The code has been moved into a separate project in our source code repository, named mojoPortal.RouteHelpers. If you compile that class library project and put the dll in the bin folder then the old way of registering routes as described below will work. However for anyone who has no current dependency on the old way we suggest use the new way documented above. Note that this class library is really a wrapper that uses the new way to register routes in order to plugin the code for the old way. So to plugin the class library you should create file named mojo-HelperRoutes.config and put it in the /Setup/ReouteRegistrars folder, with the following contents which will plugin the class library:

<?xml version="1.0"?>
<Routes>
    <IRegisterRoutes type="mojoPortal.RouteHelpers.RouteRegistrar, mojoPortal.RouteHelpers" />
</Routes>

Since this routing sytem was not used for anything we ship, it is disabled by default so the first step is to add this in your user.config then touch web.config or recycle the IIS application pool to restart the application.

<add key="EnableRouting" value="true" />

Routes have to be registered in the application start event so we have implemented a way to plugin routes by configuration files which will be processed in the application start event to register the configured routes. The configuration files need to be placed in the folder /Setup/Routes

There is a file already existing in that folder named mojoRoutes.config that has examples of the syntax you would use to configure your own routes.

Last Modified by Elijah Fowler on Jul 29, 2019