Hi all
I've been working on my site and found a good way to put the site structure into a model and render it from that. Also I use caching to store that model to make the page load faster.
I wrote a blog post about it. Take a look and let me know what you think, or if there is anything you would add.
I just read the article and here are some things that I would have done differently. That's doesn't mean it's a better :-)
Use MVC convetions for viewpath
I would have created the view in a folder ~/Views/SiteLayout/. Then you can do and MVC will pick up the path because the view is located in a folder with the name of the controller
return PartialView("_TopNavigation.cshtml", nav);
Query content instead of using path level
You use a fixed level for your homepage and then use the path to determine it. I would have just queried the content to get the homepage.
Hi Dave
Thanks for your detailed reply. I really appreciate it.
Regarding your points:
Use MVC convetions for viewpath
Thanks for pointing this out. I tried this in the past but didn't have much success. I put it down to Umbraco's routing being weird. This is clear now, I will do that from now on.
Query content instead of using path level
You are right, I normally use dynamic typing, but wanted to used strongly typed and couldn't find a way to get it to work. Thanks for your example.
Use strongly typed content access instead of dynamic
Again, similar to the last, I couldn't find the visible property to use in a lambda, should have looked for IsVisible. Thanks for this, I will definitely use it.
I couldn't find the point you were trying to make about the redundant checks.
Caching
The nav does need active state.
I didn't want to use the built in caching from Umbraco because I wanted to use the simple example for caching that would work outside of Umbraco.
Thanks again, hopefully someone will see this thread and learn a lot from it.
Thanks Paul for this interesting blog. Very helpful.
However, it may sound stupid, but how would you go about adding related links for example home, blogs etc.. using cache implementation for navigation model.
Site navigation model example with caching
Hi all I've been working on my site and found a good way to put the site structure into a model and render it from that. Also I use caching to store that model to make the page load faster.
I wrote a blog post about it. Take a look and let me know what you think, or if there is anything you would add.
http://www.codeshare.co.uk/blog/umbraco-site-navigation-menu-model-example-in-c-mvc/
Cheers
Paul
Hi Paul,
I just read the article and here are some things that I would have done differently. That's doesn't mean it's a better :-)
Use MVC convetions for viewpath
I would have created the view in a folder ~/Views/SiteLayout/. Then you can do and MVC will pick up the path because the view is located in a folder with the name of the controller
Query content instead of using path level
You use a fixed level for your homepage and then use the path to determine it. I would have just queried the content to get the homepage.
Use strongly typed content access instead of dynamic
Your method to get childitems takes a dynamic parameter page. I would have used strongly typed IPublishedContent
Then you can do you visible check like this
Also you have some redudant checks in this method : childPages.Any() && childPages.Count > 0
Read this article about the difference between dynamic and stronlgy typed content access : http://24days.in/umbraco/2015/strongly-typed-vs-dynamic-content-access/
Caching
For data caching I would have used built in caching from Umbraco : https://our.umbraco.org/documentation/Reference/Cache/updating-cache
If the menu doesn't need a active state I would probably have used (donut) outputcache only. So it's cached once for the entire website.
If the navigation needed a active state that would have used both memory and output cache.
Another thing is also I don't see anything to clear your cache when a editor makes changes in the backend.
That's my input. Hope you will find it usefull
Hi Dave Thanks for your detailed reply. I really appreciate it. Regarding your points:
Use MVC convetions for viewpath Thanks for pointing this out. I tried this in the past but didn't have much success. I put it down to Umbraco's routing being weird. This is clear now, I will do that from now on.
Query content instead of using path level You are right, I normally use dynamic typing, but wanted to used strongly typed and couldn't find a way to get it to work. Thanks for your example.
Use strongly typed content access instead of dynamic Again, similar to the last, I couldn't find the visible property to use in a lambda, should have looked for IsVisible. Thanks for this, I will definitely use it.
I couldn't find the point you were trying to make about the redundant checks.
Caching
The nav does need active state. I didn't want to use the built in caching from Umbraco because I wanted to use the simple example for caching that would work outside of Umbraco.
Thanks again, hopefully someone will see this thread and learn a lot from it.
Paul
Thanks Paul for this interesting blog. Very helpful.
However, it may sound stupid, but how would you go about adding related links for example home, blogs etc.. using cache implementation for navigation model.
One small thing I noticed was that you had a redundant check on this property on the
NavigationListItem
class:You don't need the
&& Items.Count > 0
after callingItems.Any()
as it will never be evaluated and is redundant.Also, you can use the built-in Umbraco classes in
ApplicationContext.ApplicationCache.RuntimeCache
for caching items rather than rolling your own.is working on a reply...