Anders Hattestad
Dec 23, 2009
  9978
(0 votes)

QueryRewriteProvider replace query strings with paths

For fun I tried to make a url rewriter that could translate generic query parameters to be part of the path.

So if you had a page /newlist/  and used a query parameter to get to page 2 like this /newslist/?page=2 you would get that parameter as a path like /newslist/page_2/.

The code to replace all links is simple when you base your code on FriendlyUrlRewriteProvider.

From pageRef –> Url

#region from PageRef=>URL
protected override bool ConvertToExternalInternal(UrlBuilder url, object internalObject, System.Text.Encoding toEncoding)
{
    bool status=base.ConvertToExternalInternal(url, internalObject, toEncoding);
    if (status)
    {
        PageReference pageLink = internalObject as PageReference;
        if (pageLink != null)
        {
            List<string> done = new List<string>(); 
            foreach (string key in url.QueryCollection.AllKeys) 
            {
                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(url.QueryCollection[key]))
                {
                    url.Path += key + "_" + url.QueryCollection[key] + "/";
                    done.Add(key);
                }
            }
            foreach (string d in done)
                url.QueryCollection.Remove(d);
           
        }
    }
    return status;
}
#endregion

This code will replace all links create on a page with the path of that page and the extra “path” from the query.

FromUrl=>Page

When  want to translate the extra path to a query parameter I need to do some small changes in ConvertToInternalInternal function. But the changes are unfortantly a bit inside the base function so I needed to copy the whole function. I also needed to make some adjustments to the helper function GetPageFromStartByPath and GetPageBySegments so I could find last know page from path and get the rest of the path so I could add them to the query collection.

protected static PageReference GetPageBySegments(PageReference rootPageRef, string[] segments, string languageCode, out PageReference lastGood, List<string> segmentsRemoved)
{
    lastGood = PageReference.EmptyReference;
    for (int i = 0; (i < segments.Length) && !PageReference.IsNullOrEmpty(rootPageRef); i++)
    {
        rootPageRef = UrlSegment.GetPageBySegment(rootPageRef, segments[i], languageCode, false);
        if (!PageReference.IsNullOrEmpty(rootPageRef))
            lastGood = rootPageRef;
        else
        {
            for (int x = i; x < segments.Length; x++)
                segmentsRemoved.Add(segments[x]);
        }
    }
    return rootPageRef;
}

Inside the ConvertToInternalIntranal I added this code

if (page == null)
{
    PageReference lastGood = PageReference.EmptyReference;
    List<string> segmentsRemoved = new List<string>();
    page = GetPageFromStartByPath(str, languageBranchAndPath,out lastGood, segmentsRemoved);

    if (page == null && !PageReference.IsNullOrEmpty(lastGood) && segmentsRemoved.Count>0)
    {
        page = DataFactory.Instance.GetPage(lastGood, LanguageSelector.Fallback(ContentLanguage.PreferredCulture.Name, true));
        foreach (string segment in segmentsRemoved)
        {

            string[] parts = segment.Split('_');
            queryCollection.Add(parts[0], HttpContext.Current.Server.UrlDecode( segment.Substring(parts[0].Length + 1)));
        }
    }
}

This code basically find the depths page based on the path, and take the rest of the path’s as query parameters. The code don’t use more cpu than the ordinary friendly rewriter.

 

The full code is here

Dec 23, 2009

Comments

Mårten Berg
Mårten Berg Sep 21, 2010 10:33 AM

Works great. Thanks for sharing.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024