Views: 9854
Number of votes: 1
Average rating:

Optimizing Friendly URL for Page Providers

Page Providers are often used to integrate flat external data without a hierarchy. Since everything in EPiServer CMS assumes you have a nice hierarchy you simulate a hierarchy by for example categories or date of creation (i.e. /MyProvider/2009/04/28/My-Data-Item/).

The FriendlyURLRewriteProvider will find the actual page by recursively calling GetChildren and looking at the property PageURLSegment. There are 2 issues you can run into:

1) The URLs are not permanent, you may base your simulated structure on something that will change very often.

2) Performance issues, your backing store is slow or contains large amount of content that you do not want to load into memory just to evaluate a URL (in case you don’t display a tree based on this structure on the site, otherwise this makes no sense since it eventually will be loaded anyway).

The approach taken by me is to:

1) Add an identifier on the PageURLSegment property for your data item so we can use that identifier to resolve pages instead of the friendly URL. This way everything plays nice with EPiServer CMS and the “default” friendly URL would continue to work even without our module.

2) Hook into the URL rewriter and take over those incoming URLs that need to be resolved and use the identifier instead.

The first step is simple, just extend your page provider and add the unique identifier (I am basing this example on the Northwind provider by Allan).

   1: pd.URLSegment += "." + pd.PageLink.ID.ToString();

 

Now we get URLs like “/MyProvider/2009/04/28/My-Data-Item.3434/” and they should work just fine, next step is to hook into the URL rewriter and take over those. I use a HttpModule for that that needs to be registered in web.config.

   1: public void Init(HttpApplication context)
   2:        {
   3:            // This method will be called multiple times for every HttpApplication, since the UrlRewriteModule has one
   4:            // instance per application with non-static events this is perfectly fine.
   5:            UrlRewriteModule m = context.Modules["UrlRewriteModule"] as UrlRewriteModule;
   6:            if(m==null)
   7:                throw new Exception("Cannot find a http module with name UrlRewriteModule");
   8:  
   9:            m.HttpRewritingToInternal += new EventHandler<UrlRewriteEventArgs>(m_HttpRewritingToInternal);
  10:        }

 

The next step is to find “our” URLs so that we can cancel the built-in resolver and make our own lookups:

   1: private void m_HttpRewritingToInternal(object sender, UrlRewriteEventArgs e)
   2:        {
   3:            if (e.Cancel || e.Url==null)
   4:            {
   5:                return;
   6:            }
   7:  
   8:            // Don't do anything when edit-mode passes id=xx since then no parsing is needed
   9:            if (e.Url.QueryCollection["id"] != null)
  10:            {
  11:                return;
  12:            }
  13:  
  14:            // Locking is handled in Init-method
  15:            if(PageReference.IsNullOrEmpty(_entryPoint))
  16:            {
  17:                Init();
  18:            }
  19:  
  20:            // Check if the path follows "our" pattern
  21:            Match match = _urlValidate.Match(e.Url.Path);
  22:            if (!match.Success)
  23:            {
  24:                return;
  25:            }
  26:  
  27:            // Now verify that the path is rooted on our page provider,
  28:            // we do not cache this path since the page could be moved
  29:            string url = GetFriendlyURL(_entryPoint);
  30:            if (e.Url.Path.ToString().StartsWith(url,true,CultureInfo.CurrentCulture))
  31:            {
  32:                PageReference pageLink;
  33:  
  34:                // Try to parse the "id" baked into the URL
  35:                if (PageReference.TryParse(match.Groups["id"].Value, out pageLink))
  36:                {
  37:                    //Get the page from our page provider
  38:                    PageData page = DataFactory.Instance.GetPage(new PageReference(pageLink.ID,PageProviderName));
  39:                    
  40:                    // Rewrite the path and keep any query strings
  41:                    Url urlStaticLink = new Url(page.StaticLinkURL);
  42:                    e.Url.Path = urlStaticLink.Path;
  43:                    e.Url.QueryCollection.Add(urlStaticLink.QueryCollection);
  44:  
  45:                    // Let EPi know we did some changes
  46:                    e.IsModified = true;
  47:                    e.Internal = page.PageLink;
  48:                    e.Cancel = true;
  49:                }
  50:            }
  51:        }

 

TODO

You need to add handling of invalid URLs to this sample. For example if someone changes the URL to “/MyProvider/2009/04/28/I-hate-you.3434/” it would continue to work since we only are looking at the identifier. You need to figure this out by comparing the incoming URL to the actual data and redirect the users to the correct URL instead, I’ve seen sites that do not handle this and its a type of ugly injection that can be misused. Consider yourself warned.

 

Download the demo source

Apr 27, 2009

Guest
(By Guest, 3/20/2017 3:17:11 PM)

Guest
(By Guest, 4/10/2017 2:34:16 PM)

Please login to comment.