Copied to clipboard

Flag this post as spam?

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


  • manila 170 posts 392 karma points
    Sep 13, 2017 @ 09:05
    manila
    0

    Trouble of url mapping in route hijacking in EventHandler

    I am trying to hijack route in EventHandler. I don't know how to map the first part of the url which is currently this:

    "the-top-parent/parent/library/{id}/{slug}"
    

    and it works without problem.

    What I would like to do instead of the above is to have:

    "{parentslug}/library/{id}/{slug}"
    

    but that doesn't work - the app cannot find the template if the first bit of the url is dynamic/ not predefined - and displays error instead of the valid page:

    Sequence contains no elements
    

    Is it possible to make the start of the url optional? And how?

    public ActionResult GetItem(RenderModel renderModel, string parentslug, int? id, string slug) {
    ...
    }
    
    public class MyStartupHandler : IApplicationEventHandler
    {
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            RouteTable.Routes.MapRoute(
                "item",
                "the-top-parent/parent/library/{id}/{slug}",
                new
                {
                    controller = "MyController",
                    action = "GetItem",
                    slug = UrlParameter.Optional
                });
        }
    }
    

    I'm using Umbraco 7.1.8.

  • Damiaan 380 posts 1045 karma points c-trib
    Sep 13, 2017 @ 09:08
    Damiaan
    1

    You could create a contentfinder.

    Especially the example on the linked page above:

    public class MyContentFinder : IContentFinder
    {
      public bool TryFindContent(PublishedContentRequest request)
      {
        var path = request.Uri.GetAbsolutePathDecoded();
        if (!path.StartsWith("/woot"))
          return false; // not found
    
        // have we got a node with ID 1234?
        var contentCache = UmbracoContext.Current.ContentCache;
        var content = contentCache.GetById(1234);
        if (content == null) return false; // not found
    
        // render that node
        request.PublishedContent = content;
        return true;
      }
    }
    

    Hope that helps.

  • manila 170 posts 392 karma points
    Sep 13, 2017 @ 10:26
    manila
    0

    Thanks but I read the article you quoted and am not sure how to implement it so I can route correctly what I need?

  • Damiaan 380 posts 1045 karma points c-trib
    Sep 13, 2017 @ 11:59
    Damiaan
    0

    Ok. Fair question. Let's turn things around. Why do you want to route to a controller? Is their a umbraco node matching the url?

  • manila 170 posts 392 karma points
    Sep 13, 2017 @ 15:07
    manila
    0

    To keep consistent with the page parent structure

  • Marcio Goularte 195 posts 596 karma points
    Sep 13, 2017 @ 17:44
  • manila 170 posts 392 karma points
    Sep 14, 2017 @ 08:07
    manila
    0

    Thanks Marcio but I'm not having problem with routing - it works fine.

    I want to make the start of url in MapRoute optional and this is what I don't know how to achieve.

    Eg. instead of

    "the-top-parent/parent/library/{id}/{slug}"
    

    I want to use

    "{parentslug}/parent/library/{id}/{slug}"
    

    and pass parentslug as parameter as well to the handling method.

Please Sign in or register to post replies

Write your reply to:

Draft