Establish site navigation hierarchy in asp.net 2.0 (4)

Source: Internet
Author: User
Tags contains end expression key net query reference urlencode

Explicit expressions enable developers to have greater control over the names of files and resource keys (resource key) that contain local resources. In the example Web.sitemap, the first element uses an explicit resource expression. An explicit expression is specified on each property. The title property of the first element uses an explicit expression. An explicit expression must begin with $resource:. After this identifier, the developer must provide the root name and resource key for the resource file. Developers can choose to provide a default value. In the example, the expression $resources:title, MyTitle, home indicates that the provider should see the resource file that starts with "Title". For browser employed that send French header information, the provider looks for Title.fr.resx resource files. The provider then looks at the resource that the key is MyTitle. If the provider cannot find this resource, it will use the string "Home" as the default value.

You can run the example to see how the site map localization works. Browsers that use English as the default language will have an explicit English text. If you use IE, you can click the "Tools->internet option" and the "language" button on the "General" tab, click the "Add" button and choose Add "French". If necessary, also select French and click the "Move Up" button to make it the default request language for IE. After you change the default language to French, refresh the sample page. Note that the text in the menu, TreeView, and SiteMapPath controls is automatically explicitly the French text in the French resource file that is stored in the App_GlobalResources directory.

The content of Web.sitemap

The following is a reference fragment:
>





Modify the site navigation data returned by the provider (Provider)

The navigation data stored in Web.sitemap for XmlSiteMapProvider use is static-the data is loaded into memory and stored as read-only data. However, the navigation structure of many sites is parameterized based on the value of the query string. For example, a newsgroup (newsgroup) site may have a well-defined page structure (for example, a home page, a news category page, and a news content page), but the actual content can vary greatly, depending on the identifier in the query string. Although it is possible to store each possible query string value in an element, even a medium number of query string values requires a sitemap file to contain hundreds of elements.

The site navigation feature exposes the Sitemapresolve event in the SiteMapProvider base class. You can perform this event by using Sitemap.sitemapresolve or by using the provider SiteMap.Provider.SiteMapResolve directly. The return value of this event is an SiteMapNode instance. You can create a hierarchy of SiteMapNode instances by writing custom logic in your event handlers. This logic can modify the properties of each SiteMapNode, so properties such as URLs and title reflect the data information that is in the query string.

The following example registers an event handler in Global.asax. The code for this event handler is a class in the App_Code directory. This custom class replicates the SiteMapNode instance that corresponds to the current page. The nodes returned by XmlSiteMapProvider are read-only, and the Clone method on the call SiteMapNode returns a writable node. In an instance, if a true value is passed to clone, it will cause the current SiteMapNode and all its parent nodes to be writable. Other parts of the code for this class check the current page and the current page's query string to determine where the current page is located in the site hierarchy. The code modifies the URL and title attributes, and contains additional information so that the navigation UI displayed by the SiteMapPath control reflects the actual click path of the site user to reach the current page.

When you run the example, you start at the site's home page. The SiteMapPath control also reflects this. Clicking on any link will take you to the category page, which displays news links in the relevant news categories. Note that if you hover your mouse over the last link in the SiteMapPath control, the URL displayed in the browser status bar contains the query string information (which specifies the news category). Clicking on any of the posting links will bring you back to the press release page. If you hover your mouse over a link to the SiteMapPath control, notice that the last two links in the control contain the URL and title containing the correct query string and descriptive information for the click Path. If you navigate to the homepage of your site and click on other newsgroups and content links, the SiteMapPath control is updated and reflects the link to the second click.

The following is a reference fragment:
Public Class Pathexpansionhandler
Public Shared Function Expandpath (ByVal sender as Object, ByVal e as Sitemapresolveeventargs) as SiteMapNode
' Get references to current and previous nodes
Dim nodeCopy as SiteMapNode = SiteMap.CurrentNode.Clone (True)
Dim Tempnode as SiteMapNode = nodeCopy

' Check If there are a newsgroup type in the query string 
Dim typeid As String = nothing  
Dim ty peidurlencoded as String = nothing  
If not String.IsNullOrEmpty (e.context.request.querystring ("type")) then 
typeID = E.context.server.htmlencode (e.context.request.querystring ("type"))  
typeidurlencoded = E.context.server.urlencode (e.context.request.querystring ("type"))  
End if 

' First execute the Publish page URL's fixed
' If the query string contains the publication ID, we know the current node publishing page
If not String.IsNullOrEmpty (e.context.request.querystring ("Postingid")) Then
Dim Postingid As String = _
E.context.server.htmlencode (e.context.request.querystring ("Postingid"))
Dim postingidurlencoded As String = _
E.context.server.urlencode (e.context.request.querystring ("Postingid"))
Dim Newurl as String = Tempnode.url + "? type=" + typeidurlencoded + "&postingid=" + postingidurlencoded
Dim Newtitle as String = Tempnode.title + ":" + postingid
Tempnode.url = Newurl
Tempnode.title = Newtitle

Tempnode = Tempnode.parentnode
End If

' Then, fix the newsgroup page
' This time the nodecopy variable is known. News Group node
If not String.IsNullOrEmpty (e.context.request.querystring ("type")) Then
Dim Newurl as String = Tempnode.url + "? type=" + typeidurlencoded
Dim Newtitle as String = Tempnode.title + ":" + typeid
Tempnode.url = Newurl
Tempnode.title = Newtitle
End If

' Finally returns the current node
Return nodeCopy
End Function
End Class



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.