Try our conversational search powered by Generative AI!

Re-routing

Vote:
 

We'd like to re-route requests from one EpiServer page to another, when parameters are add to the url

http://abc.com/productCategory - goes to product listings page

http://abc.com/productCategory/title/id - goes to product details page

We've implemented this so far using a Paritial Router but the correct page is not found. We followed the example here -

http://joelabrahamsson.com/custom-routing-for-episerver-content

We can see the code being called, the correct segments are available, and the page we want to route to seems to be found and returned with no issues.

This is an example of our implementation of the router method.

public class ProductDetailsPartialRouter : IPartialRouter
    {
        public object RoutePartial(ResponsiveLandingPage content, SegmentContext segmentContext)
        {
            if (string.IsNullOrEmpty(segmentContext.RemainingPath))
            {
                return content;
            }

            ProductDetailsPage productDetailsPage = SiteConfiguration.SiteSettings.ProductDetailsPage.GetPage();
            
            if (productDetailsPage != null)
            {
                segmentContext.RoutedContentLink = productDetailsPage.ContentLink;
            }

            return productDetailsPage;
        }

        public PartialRouteData GetPartialVirtualPath(ProductDetailsPage content, string language, RouteValueDictionary routeValues,
            RequestContext requestContext)
        {
            return null;
        }
    }




Do we also implement GetPartialVirtualPath when we don't have any outgoing links?
#152254
Edited, Aug 22, 2016 16:50
Vote:
 

Hmm I think you need to set the remaining path as well I think. Otherwise Episerver will keep trying to resolve it since there are still url segments left to parse...

#152265
Aug 22, 2016 22:18
Vote:
 

We've tried that as well now, but are still getting the same response.

We return the page, but end up getting a 404

#152306
Aug 23, 2016 15:33
Vote:
 

Hi.

To Daniels note: Are you setting the remaining path to an empty string (remove you managed segment)? And is the RoutePartial method hit after you return your Product? If yes, then EPiServer may continue resolving because it's not necessarily done with the entire URL path

          //The format we handle is "productname-id/"
            
            //Use helper method GetNextValue to get the next part from the URL
            var nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath);

            int productId;
            var segments = nextSegment.Next.Split('-');

            if (int.TryParse(segments.Last(), out productId))
            {

                IProduct productContent = _productStore.GetProductForCurrentUser(productId);

                if (productContent != null)
                {
                    //Verify if the URL is the right one

                    string url = this.GetUrlSegment(productContent);

                    if (!url.Equals(nextSegment.Next))
                        return false;
                   
                    //Update RemainingPath so the part that we have handled is removed.
                    segmentContext.RemainingPath = nextSegment.Remaining;

                    return productContent;
                }
            }

            return null;

Above code is an example taken out of a greater context. Be aware that it haven't been tested after being altered, but I hope it can provide some guidance.

Have a good day.

Casper Aagaard Rasmussen

#152307
Aug 23, 2016 16:16
Vote:
 

I've tried that as well, but still getting the same result. Even tried just setting RemainingPath to string.Empty on each call. The method gets called twice and the path value is populated both times

I'm setting segmentContext.RemainingPath, segmentContext.RoutedContentLink, and returning the page to be re-routed to.

#152308
Aug 23, 2016 16:46
Vote:
 

From what you describe that you actually get to the partialrouter and that you find the content you want to route to I can't think of anything but failing to set the segmentContext.RemainingPath. If that's not it I'm officially out of ideas :)

#152309
Aug 23, 2016 16:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.