Copied to clipboard

Flag this post as spam?

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


  • Megan Duffy 2 posts 72 karma points
    Feb 03, 2017 @ 15:29
    Megan Duffy
    0

    Route hijacking route still valid after custom route implemented

    I have added a custom route (/catalog/{x}/{y}) for my Product page. I can successfully use the custom route to view the page. However, when I navigate to /product I also see the Product page. I don't want the route hijacked route to work anymore. Is there a way to disable it?

    I have a Product document type, ProductController that returns View("Product"), and Product.cshtml view.

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Feb 04, 2017 @ 10:15
    Dave Woestenborghs
    0

    Hi Megan,

    Can you post the code on how you created the custom route and the code of your controller ?

    That's easier to spot the issue.

    Dave

  • Megan Duffy 2 posts 72 karma points
    Feb 06, 2017 @ 14:33
    Megan Duffy
    0

    Here is the custom route code:

    public class CustomRoutingEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var product = RouteTable.Routes.MapUmbracoRoute(
                "Product",
                "catalog/{x}/{y}",
                new { controller = "product", action = "index" },
                new CustomRouteNodeRouteHandler("product"));
        }
    }
    
    public class CustomRouteNodeRouteHandler : UmbracoVirtualNodeRouteHandler
    {
        private string _documentTypeAlias;
    
        public CustomRouteNodeRouteHandler(string documentTypeAlias)
        {
            _documentTypeAlias = documentTypeAlias;
        }
    
        protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
        {
            var helper = new UmbracoHelper(umbracoContext);
            return helper.TypedContentAtRoot()
                .First()
                .Descendants()
                .First(d => d.DocumentTypeAlias.InvariantEquals(_documentTypeAlias));
        }
    }
    

    This associates the route catalog/{x}/{y} with my "Product" document type. Here is the controller for that page:

    public class ProductController : RenderMvcController
    {
        public ActionResult Index(RenderModel renderModel, string x, string y)
        {
            var model = new ProductModel(renderModel.Content);
            return View("Product", model);
        }
    }
    
  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Feb 07, 2017 @ 11:11
    Dave Woestenborghs
    0

    Hi Megan,

    What I would do in this case is setup a second action, for example product and use that for the virtual route.

    Than in the index method I would return a 404 or redirect to the parent.

    dave

  • 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