Developer forum

Forum » Development » URL provider used multiple times

URL provider used multiple times

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

We're running into an issue with a custom URL provider. The provider itself is simple, looking for "Country" (2 letter code) and "State" (2 letter code) to add "/us/california" to the URL instead of "&country=us&state=ca". It takes no parameters.

 

It all works fine when it's applied to a single page, but when I apply this to 2 different pages, both the frontend and backend become broken. I have to go through SSMS and delete one of the references to it. (check attachment for error)

Does anyone know what I might be doing wrong?

 

I'm attaching the source code in case anyone wants to take a closer look, but here's the code as well

using System.Collections;
using System.Collections.Generic;
using Dynamicweb.Ecommerce;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using Dynamicweb.Frontend.UrlHandling;

namespace Dna.Winnebago.UrlProvider
{
    [AddInName("Country and State")]
    [AddInOrder(1000)]
    public class CountryAndState : UrlDataProvider, IDropDownOptions
    {
        public override IEnumerable<UrlDataNode> GetUrlDataNodes(UrlDataNode parent, UrlDataContext dataContext)
        {
            var nodes = new List<UrlDataNode>();

            foreach (var country in Services.Countries.GetCountries())
            {
                nodes.Add(new UrlDataNode()
                {
                    Id = $"CountryAndState_{country.Code2}",
                    ParentId = parent.Id,
                    PathName = $"{country.Code2}",
                    IgnoreInChildPath = false,
                    IgnoreParentPath = false,
                    QueryStringParameter = "Country",
                    QueryStringValue = country.Code2,
                    QueryStringExact = string.Empty,
                    IgnoreParentQuerystring = false
                });

                foreach (var state in country.Regions)
                {
                    nodes.Add(new UrlDataNode()
                    {
                        Id = $"CountryAndState_{country.Code2}_{state.RegionCode}",
                        ParentId = $"CountryAndState_{country.Code2}",
                        PathName = $"{state.Name}",
                        IgnoreInChildPath = false,
                        IgnoreParentPath = false,
                        QueryStringParameter = "State",
                        QueryStringValue = state.RegionCode,
                        QueryStringExact = string.Empty,
                        IgnoreParentQuerystring = false
                    });
                }
            }

            return nodes;
        }

        public Hashtable GetOptions(string dropdownName)
        {
            var options = new Hashtable();
            return options;
        }
    }
}

 

Additionally we noticed something odd. The Url provider only worked after the site recycled. In other words:

  • Placed the dll (site recycles automatically)
  • Go to page and set the URL provider
  • Go to test and does not work
  • Recycle the site again (now that it's assigned to the page)
  • Perform the same test - it works

I don't seem to have this experience with the Ecom

 

Thank you

Nuno Aguiar

2021-08-18_17-36-53.gif

Replies

 
Nicolai Pedersen
Reply

The node ID has to be unique - for the entire solution. So when you insert it twice, your current id format will be there twice:

Id = $"CountryAndState_{country.Code2}"

So include its parent in the id:

Id = $"CountryAndState_{country.Code2}_{parent.Id}"
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

Thank you for the quick answer. That fixed it, but I still needed to recycle the site after having applied the URL provider. Is that an expected behavior?

 

I realize that these nodes are likely created on ApplicationStart, but maybe when a Page is saved comparing the before and after value of PageUrlDataProvider and PageUrlDataProviderParameters and if they are different it could rebuild them OR just the nodes associated with the current page id? I am just spitballing. I don't know exactly what happens behind the scene.

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum