Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new MvcSitemapProvider node at runtime

I'm working on a webshop-like asp.net mvc 4 website with a wcf-service datalayer. My application is build with maincategories, subcategories and products. Each product can only be in one subcategory and my url's are like this:

/maincategoryname/subcategoryname/{productid}/producttitle

And the corresponding breadcrumb trail:

Home > Maincategory > Subcategory > Producttitle

I'm currently using MvcSitemapProvider to generate my navigation menu's and breadcrumbs. I'm loading all the url's as dynamic nodes without cache. This solution works for a couple of products but when I add 1000 products the sitemap takes 6,5 second to populate, wich is way too long.

I turned on caching in MvcSitemapProvider. This way the application loads much faster. But when a user adds a new product and navigates to this new product (page). The url is not yet in the sitemap file because it uses cache. This way my navigation and breadcrumbs are not generated.

My question is:

Is it possible to add a new node to the sitemap at runtime after a user adds a new product?

like image 832
Joppe Avatar asked Feb 05 '13 13:02

Joppe


1 Answers

The accepted answer is now a little out of date. In MvcSiteMapProvider v4, there is no longer a GetCacheDescription() method in a DynamicNodeProvider. This didn't seem to work anyway.

You can now invalidate the cache manually by using the [SiteMapCacheRelease] attribute on the action methods that update the data:

[MvcSiteMapProvider.Web.Mvc.Filters.SiteMapCacheRelease]
[HttpPost]
public ActionResult Edit(int id)
{

    // Update the record

    return View();
}

Or by calling a static method:

MvcSiteMapProvider.SiteMaps.ReleaseSiteMap();

You also have the option now to extend the framework to supply your own cache dependencies.

like image 195
NightOwl888 Avatar answered Sep 28 '22 04:09

NightOwl888