Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Hartley 8 posts 78 karma points
    Nov 14, 2019 @ 18:05
    Jonathan Hartley
    0

    How do I set up a custom route in Umbraco 8?

    I'm trying to set up a custom route in Umbraco 8 by following the instructions on the following page: https://our.umbraco.com/documentation/reference/routing/custom-routes

    As well as consulting the following forum topic: https://our.umbraco.com/forum/umbraco-8/96167-registering-custom-routes-in-umbraco-8

    I created a new folder in my project called Components, and then created a new file in that folder called RegisterCustomRouteComposer.cs. The contents of that file are as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Umbraco.Core.Composing;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    
    namespace Umbraco8.Components
    {
    
        public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
        { }
    
        public class RegisterCustomRouteComponent : IComponent
        {
            public void Initialize()
            {
                RouteTable.Routes.MapUmbracoRoute("PdfCustomRoute", "pdf/{fileName}", new
                {
                    controller = "Pdf",
                    action = "Index"
                }, new UmbracoVirtualNodeByIdRouteHandler(1105));
            }
    
            public void Terminate()
            {
            }
        }
    }
    

    Is this the correct setup for a route that I want to go to the route pdf/some-pdf-file-name? Also, what is the 1105 in the UmbracoVirtualNodeByIdRouteHandler instantiation supposed to point to? I got the 1105 from the example, but I assume I need to change that to something relevant to my specific project.

    After creating the component file, I then create a folder called Controllers and created a new file in that folder called PdfController.cs. The contents of that file are as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Models;
    
    namespace OSU_OIA.Controllers
    {
        public class PdfController : Umbraco.Web.Mvc.RenderMvcController
        {
            // GET: Pdf
            public ActionResult Index(ContentModel model, string fileName)
            {
                return View("PageNotFound");
            }
        }
    }
    

    For the time being, I'm trying to just return the custom 404 Page Not Found template I've created to confirm that the route is working. However, when I go to a URL on the site such as /pdf/asd.pdf, I get an IIS 404 error. Everything builds correctly without any errors.

    What am I missing here? Am I on the right track or completely off? Any help would be greatly appreciated. Thank you.

  • Graham Davis 110 posts 376 karma points
    Nov 15, 2019 @ 18:59
    Graham Davis
    0

    I did my custom routes differently and have posted it below. Offhand.....Assuming everything else is right for you, try changing your controller

    from:

    public class PdfController : Umbraco.Web.Mvc.RenderMvcController

    to:

    public class PdfController : SurfaceController

    The way I did it was to create a file at

    /App_Start/RouteConfig.cs

    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace zBestPM
    {
        public partial class RouteConfig
        {
            public static void Register_zpmRoutes()
            {
                //Create a custom routes
                RouteTable.Routes.MapRoute(
                       name: "PropertyDetailsShortURL",
                       url: "real-estate/{propertyID}",
                       defaults: new { controller = "Property", action = "Index", propertyID = UrlParameter.Optional }
                   );
    
                RouteTable.Routes.MapRoute(
                       name: "PropertyDetails",
                       url: "real-estate/{description}/{address}/{propertyID}",
                       defaults: new { controller = "Property", action = "Index", propertyID = UrlParameter.Optional }
                   );
            }
        }
    }
    

    and then created a Global.asax.cs in the root:

    using System;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using zBestPM;
    
    namespace Umbraco8.Components
    {
        public class zpmConfigRouteRouteComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<zpmConfigRouteComponent>();
            }
        }
    
        public class zpmConfigRouteComponent : IComponent
        {
            public void Initialize()
            {
                RouteConfig.Register_zpmRoutes();
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies