Okay, so the context of the issue – I’ve been asked to do some custom URL mapping in Sitecore. The customer wants some of their pages to appear at URLs other than that defined by their site structure. There’s a good article about this here, and my URL resolver is working just fine.
However, as soon as I change my link provider to my new custom one, I get the following error:
Model too deep. Potential lazy loading loop.
Okay, what is this? Well, Glass is trying to save your bacon by preventing loops of loading models. If you exceed 8 levels of depth, then it throws a wobbly. This is a good thing.
If you want, you can turn this off (hint: the code you need to add goes into GlassMapperScCustom.cs). I tried this – and got a stack overflow. There is an infinite loop. Like I said, the check is a good thing.
Stepping through the code, I noted that when my custom link provider tried to load the model for the page I wanted to generate a custom link for then the debugger would ‘skip’ back to the link provider. This was puzzling.
Eventually, I realised that the model for my page had a base model that contained a URL field:
[SitecoreInfo(SitecoreInfoType.Url)]
string Url { get; set; }
Generating the value of this field will use the LinkProvider, which will try to get the model, which has a URL, so it will use the LinkProvider, and so on. Round and round we go.
I changed my base template – I don’t need the item’s URL in my code – and the page sprang back into life. Inifinite loop broken.