📜 ⬆️ ⬇️

ASP.NET: Extend the SiteMapPath path mechanism

When developing on ASP.NET, we are used to using standard components for standard tasks. One of these standard tasks is to display on each page of the site a path to it from the main page - a fragment of the site map (sitemap path). This is done using the SiteMapPath control, which takes the data on the site map from the site map provider (SiteMapProvider) by default, which, in turn, takes them from the Web.siteMap configuration file.

By placing this control on the master page of the site, and having described the site map in the configuration file, we thereby provide the output of the path on all pages.

But ASP.NET is an extensible environment, and allows you to replace almost all standard modules, and the sitemap provider is no exception. We will look at how to create your provider to get advanced functionality for the site map.
')

The standard component of SiteMapPath, which shows the user's current position on the site on the page, is limited by the fact that the links displayed by him are static text taken from the web.sitemap configuration file. Thus, you can display the path “Albums >> Viewing an album >> Viewing a photo” on the page with its help, but you can substitute dynamic data there, for example “Albums >> A trip to Crimea 2007 >> Vova jumps from a pier” is no longer possible.

In general, writing a similar control that displays “what they say” is a short matter, but it is interesting to make it do a standard SiteMapPath.

To do this, you must implement your own version of the SiteMapProvider class.

To state the problem, consider a classic example of a sitemap file fragment:

sitemap 1

Having such a sitemap, we will see the following path on the iPhone description page: “Catalog >> Description of the goods”. But it would be more interesting if it looked like the "Catalog >> Description iPhone". Intuitively, this implies the existence of some such sitemap:

sitemap 2

But if we imagine that there are even more logically nested pages for describing the product, for example, there is “iPhone >> Description >> Photographs”, then the user can go to the “Photographs” section to return to previous page. It becomes clear that the correctness of the link “Description of the iPhone” is also needed, which for now looks like url = “good.aspx”, i.e. does not indicate any particular product. Thus, it turns out that we have to add some dynamic fragments to the url, and the resulting “imaginary” siteMap looks like this:

site map final

So, the task looks like this: write SiteMapProvider, which supports the extended siteMap syntax given above, to display dynamic content in SiteMapPath .

Another interesting question is where to get this very contextual dynamic information. In the realities of my work, the page usually has a property that represents the business object that it shows. For example, on the Good.aspx page, there is a Good property of the Good type, representing its displayed Item object. Therefore, it is convenient that the contents of a fragment of a meta substitution ([$ ... $]) are interpreted as a property path, then expressions of the form Good.ID and Good.Name refer to the ID and Name properties of a business entity represented through the Good property of the current page (good .aspx).

In your case, it may be convenient to take this data from other sources or by other methods - this is easy to implement, the meta-substitution will be well localized in the code of our developed class.

To implement your provider, it is convenient to inherit from the standard SiteMapProvider and override a number of properties and methods. In order not to bore the reader with technical details, I will give the code of the resulting class in the attachment.


Provider code file: DynamicXmlSiteMapProvider.cs
I hope someone will come in handy :-)


Cross-post from Gendix blog

Source: https://habr.com/ru/post/30130/


All Articles