Copied to clipboard

Flag this post as spam?

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


  • Clickfarm 77 posts 161 karma points
    Feb 05, 2015 @ 15:58
    Clickfarm
    0

    custom route for data in custom admin section

    I have a custom admin section which manages a list of "Products". I would like to be able to view these products in the front-end with a custom route such as:

    mywebsite.com/products/my-product-name-1 mywebsite.com/products/my-product-name-2

    what is the easiest way to hook up a custom route to do this using umbraco 7.2.1?

    thanks!

  • Tobias Klika 101 posts 570 karma points c-trib
    Feb 05, 2015 @ 23:26
    Tobias Klika
    100

    Hi.

    I have done almost the same already in Umbraco 7.

    You should create a node (new docType) in your content tree (which acts as your "details" page). In the assigned controller you can get the Request.Url.PathAndQuery (or which part you need) and parse it yourself!

    You will alos need to use the ContentFinder. See here for an example: https://our.umbraco.org/documentation/Reference/Request-Pipeline/IContentFinder

    You can do the following with the ContentFinder:
    If the URL starts with /products/ but ends with something else (like your my-product-name-2), you basically say that the published content of your request should be the overview page (by assigning its ID (from your new details page)).
    By writing this Umbraco will automatically call the controller of your page (the ID you have entered before) and you can parse the URL.

    Hope this helps, bye Tobi

  • Clickfarm 77 posts 161 karma points
    Feb 10, 2015 @ 16:59
    Clickfarm
    0

    Thanks for your reply but I'm a little confused.

    when you say "controller" are you referring to the Template for that DocumentType?

    I'm trying to figure out where exactly these code changes need to be implemented.

    Thanks

    Edit: Got it sorted out. thanks for your assistance. I've added the appropriate code to my Template (aka Controller) and it's working now. thanks!

  • Clickfarm 77 posts 161 karma points
    Feb 10, 2015 @ 19:49
    Clickfarm
    0

    Anyone who needs this in the future, here's a little bit more detail with a code example:

    create a class with the following:

    /// <summary>
    /// This manages custom routing for product detail pages
    /// </summary>
    public class ProductDetailContentFinder : IContentFinder
    {
        public bool TryFindContent(PublishedContentRequest request)
        {
            var productDetailURL = "/products/";
            var path = request.Uri.PathAndQuery;
    
            // if path starts with the product detail url but is also longer than
            if (path.StartsWith(productDetailURL) && path.Length > productDetailURL.Length)
            {
                var contentCache = UmbracoContext.Current.ContentCache;
                var content = contentCache.GetById(1165);
                if (content == null)
                    return false;
    
                request.PublishedContent = content;
                return true;
            }
    
    
            return false;
    
        }
    }
    

    then insert the ContentFinder into the routing engine:

    public class ApplicationEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // insert custom routing for product detail pages
            ContentFinderResolver.Current.InsertType<ProductDetailContentFinder>();
        }
    }
    

    and then you will be able to pick up the custom path being sent to the template:

    // if the path was /products/my-product-name then this variable would have a value of "my-product-name"

    var productName = Request.Url.PathAndQuery.Split('/').Last();

Please Sign in or register to post replies

Write your reply to:

Draft