Asp.net| site
The site navigation provider in the site navigation provider--asp.net 2.0 exposes navigation information for pages in the application, allowing you to define the structure of the site individually, regardless of the actual physical layout of the page. The default site navigation provider is xml-based, but you can also expose the information from any back-end location by writing a custom provider.
The site navigation api--site Navigation API is used to access site navigation information in the application's code, which extracts the details of the navigation information store. You can use APIs to programmatically access the application's navigation nodes.
Navigation controls-navigation controls provide a common UI for navigating between pages, such as tree views, menus, and breadcrumb ("breadcrumbs", a control that displays the current page). These controls use the site navigation service in ASP.net 2.0 to retrieve the structure you have defined for your site. The SiteMapDataSource control also allows you to bind other UI controls to site navigation data.
Web sites often need to display navigation data to guide users on how to use the site. Asp. NET allows developers to simply define navigation data and to display the UI based on that information.
The site Navigation API is a provider-based (provider) programming content that is used to access site navigation data. The API stores navigation data in an XML file and exposes the data through a set of SiteMapNode classes. Application and control developers can build SiteMapNode instances and use this information to display the navigation interface.
Navigation-oriented server controls include menu, TreeView, SiteMapPath, and SiteMapDataSource controls. These controls are built at the top of the site navigation class, and they use and display navigation data regardless of the specific details of the data store. The menu and TreeView controls can also use data from XML files and XmlDataSource controls.
The URL mapping attribute allows developers to define simple rules for the remapping (re-mapping) of different URL requests.
Using Site navigation controls
The Menu, TreeView, SiteMapPath, and SiteMapDataSource controls generate navigation interfaces based on navigation data. Navigation data can be stored in an XML file, or by leveraging the capabilities of the site navigation feature based on the provider. The following example shows how to combine different controls that use site navigation features.
Building an application Site map
The example navigation structure is stored in the Web.sitemap file, where you can see the sitemap file. The Web.sitemap file contains a top-level element. Nesting at least one element within the element. You must have a top-level within a site map. The site navigation feature requires a root to ensure that access along the node hierarchy eventually converges to a known node. You can nest multiple elements under the root element. In addition, the depth of the nested element is unrestricted.
A element typically contains a URL (link), title (title), and description (description) attribute. The URL property indicates the path that corresponds to the page in the application. It can also contain the path to a page in another application, or to multiple URLs for a completely different Web site. In the following example, all URL properties use the application-relative syntax to refer to the path. The Title property is used to display the text content of the navigation data UI. For example, the SiteMapPath control displays the title property as the hyperlink text for the control. If the Description property is provided, the server control displays as a ToolTip or alt text. Developers can also add custom attributes to , using the default indexer (indexer) of the SiteMapNode class to retrieve these properties. You can check it out. NET Framework component documentation to find more information about some other properties of the element.
The content of Web.sitemap
......
Using Site navigation controls
The easiest way to provide site navigation on a Web page is to use a graphical site navigation control (SiteMapPath, TreeView, and Menu).
· sitemappath--a breadcrumb control that retrieves the user's current page and displays the hierarchical structure of the page. This allows the user to navigate back to other pages in the hierarchy. SiteMapPath can only be used with SiteMapProvider, you need to set the control's SiteMapProvider property.
· treeview--provides a vertical user interface on a Web page that expands and shrinks selected nodes, or provides check box functionality for users to select data items. The TreeView control supports declarative or programmatic settings for data elements and data source controls. If you use the SiteMapDataSource control, data binding is done automatically.
· menu--provides a horizontal or vertical user interface that pops up when the user puts the mouse over an item. The menu control supports declarative or programmatic settings for data elements or data source controls. If you use the SiteMapDataSource control, data binding is done automatically.
Note that both the TreeView and the menu controls can be used in situations that are not navigable.
The following table describes the differences between the TreeView and the menu so that you can make the appropriate choices as needed.
Attributes Menu TreeView
Extended Pop-up location extension
Download No Yes on Demand
check box No Yes
Template Yes No
Layout Horizontal & Vertical Vertical
The style option Yes Yes
Select the mode static, dynamic hierarchy or parent/root/leaf, or by data item
In the following example, the TreeView and menu controls you see Configure some style properties for different areas and actions, such as NodeStyle and HoverNodeStyle. The TreeView and Menu controls in the example are connected to the same SiteMapDataSource control on the page. The SiteMapPath control accesses the same data through the default SiteMapProvider set in the Web.config file.
Using the Site Navigation API
The site Navigation API is a programmatic abstraction that uses a configurable provider to access navigation data. The site navigation provider isolates the storage details of navigation data from other parts of the API. The Site navigation API exposes navigation data through Sitemap and SiteMapNode classes. The Sitemap class returns the SiteMapNode instance corresponding to the current page. It also has access to the providers that are configured for site navigation features. SiteMapProvider provides a rich API for performing some of the following transactions:
· Retrieves the SiteMapNode instance based on the current HttpContext or any URL.
· Retrieves the parent or child node of the SiteMapNode.
· Accesses the SiteMapNode of the current page and the root SiteMapNode in the entire navigation hierarchy.
· Execute authorization rules, which ensures that the provider only returns nodes that the user can see.
The basic navigational information and features exposed by the SiteMapNode instance include:
· URLs, title and description properties, and custom attributes that developers add to SiteMapNode.
· Gets the parent and child nodes of a node.
· Navigate between the front and back nodes of a node.
· Gets a pointer to the SiteMapProvider instance, which returns a node.
Asp. NET is published with the XmlSiteMapProvider provider. The provider uses the data in the XML file (Web.sitemap) and returns SiteMapNode instances based on the data. XmlSiteMapProvider has some of the following features:
· Multiple site Map (sitemap) files can be linked together to build a "virtual" navigation data collection.
· Multiple XmlSiteMapProvider instances can be linked together to build a "virtual" navigation data collection.
· The provider can return the overly-worrying node based on the current file authorization and URL authorization rules for the site.
With the SiteMapProvider pointer, you can retrieve the site navigation data for a specific node based on the URL. It will let you get the SiteMapNode instance pointer in the site navigation data. The ability to retrieve arbitrary SiteMapNode instances, combined with the ability to start site navigation from any SiteMapNode, allows you to easily traverse the navigation data of the site.
As a developer, you can also store navigation data in other formats (for example, as relational data is stored in a database). Then you should build a custom provider that derives from SiteMapProvider.
Programming with site Navigation classes
You can programmatically obtain navigation data in your code. The starting point for programmatic access to site navigation data is the Sitemap class. There are a number of static methods in this class, the most important of which is the CurrentNode attribute. On any page of the site, you can invoke Sitemap.currentnode to refer to a piece of navigation data that matches the current execution page. The navigation data is returned in the form of a SiteMapNode instance-when you call Sitemap.currentnode, the property returns the SiteMapNode instance corresponding to the current page. The site navigation feature returns the correct node based on the navigation data stored in the XML file.
The following example shows a user control with a simple paging feature. In the displayed page, the user control is located in the middle of the bottom of the page. Initially, the content of the link is "next topic." When you click on this link, the user control invokes the SiteMapNode object to detect if there is a page near the current page. The code detects the Sitemap.currentnode property to see if there is a page (SiteMap.CurrentNode.PreviousSibling) in front of it, Whether it has a page (SiteMap.CurrentNode.NextSibling) behind it. If a page exists, the user control displays a hyperlink and sets the NavigateUrl property of the hyperlink to the URL property of a nearby node.
If you click on the TreeView link on the left side of the page, you can see how the user control automatically displays the appropriate "previous topic" and "later theme" links. The user control also displays a different hyperlink, which you can click to return to the home page. If you look at how this hyperlink works, you'll find that the control leverages the custom attribute "CustomAttribute" in the home page element. The control demonstrates how to use the SiteMapNode default indexer to retrieve the value of a custom property.
If (not SiteMap.CurrentNode.PreviousSibling are nothing)
Prevtopic.navigateurl = SiteMap.CurrentNode.PreviousSibling.Url
Else
Prevtopic.visible = False
Separator.visible = False
End If
' Use Findsitemapnode to find the nodes in the URL and extract some information
Dim RootNode as SiteMapNode = SiteMap.Provider.FindSiteMapNode ("~/home.aspx")
Gohome.navigateurl = Rootnode.url
Gohome.tooltip = Rootnode.description
Gohome.text = RootNode ("CustomAttribute")
End Sub
Security for site Navigation
The site navigation attribute can be based on the SiteMapNode instance returned by the authorization rule-out provider. XmlSiteMapProvider can worry about nodes based on the file and URL authorization rules used by the current Web site.
The following example uses the form authorization rule, and the predefined user credentials are stored in the web.config. In Global.asax, the user's role is appended to the current request, depending on the user name. In Web.config, the securityTrimmingEnabled property of the element used by the site map provider that is nested under the element is set to true. Also, a set of URL authorization rules is defined at the end of the Web.config file. When you run the example and log in, XmlSiteMapProvider automatically performs authorization detection on the SiteMapNode based on the roles that the user belongs to and the authorization rules defined in Web.config.
Use one of the following three accounts to run the sample:
· Userid:sectionone Password:sectionone
· Userid:sectiontwo Password:sectiontwo
· Userid:allsections password:allsections
There is an "exit" link in the upper-right corner of the page, so you can log in and out with a different account. Note that depending on the account number you log in to, the TreeView and Menu controls displayed by the navigation UI automatically reflect the access rights that the user has received. Providers automatically worry about returning nodes--no additional code is required to implement this functionality. When you log on with the "Sectionone" account, only the "Sectionone" link and the external link are displayed in the TreeView control on the left. When you log on with the "Sectiontwo" account, only the "Sectiontwo" link and the external link are displayed in the TreeView control on the left. When you log in with the "allsections" account, all the links are displayed in the TreeView control. Authorization rules in Web.config are configured to grant partial access to the "Sectionone" and "Sectiontwo" levels.
This example also demonstrates how to handle URL security outside the scope of the application directory. In the Web.sitemap file, the external linked node uses the Roles property. The syntax roles= "*" grants all users the right to access and view nodes in the navigation control. Syntax roles= "adminstrators,regular users" only allows users of these roles to retrieve and view the nodes in the navigation control. Because the Global.asax file in the example divides the user into both roles, you can always see the external link.
Developers can choose to use both file/url authorization rules and Roles properties to control user access to SiteMapNode instances. If the setting information for both is correct, the site navigation provider authenticates the current user based on the role of the file/url authorization rules and roles properties. If the current user passes any one of the authorization checks, the node can be accessed.
If the default security action does not apply to your application, developers can also xmlsitemapprovider derived classes and overload the Isaccessibletouser method with a custom node authorization implementation.
The content of Web.config
<system.web>
<authentication mode= "Forms"
<forms name= ". Aspxauth "loginurl=" Login.aspx "protection=" All "timeout=" path= "/" requiressl= "false" slidingexpiration= "true" Defaulturl= "Home.aspx" cookieless= "UseCookies" enablecrossappredirects= "false"
<credentials passwordformat= "Clear"
<user name= "Sectionone" password= "Sectionone"
<user name= "Sectiontwo" password= "Sectiontwo"
<user name= "allsections" password= "Allsections"
</credentials>
</forms>
</authentication>
<authorization>
<deny users= "?" />
</authorization>
</system.web>
<location path= "Sectionone.aspx"
<system.web>
<authorization>
<allow users= "Sectionone" roles= "Administrators"
<deny users= "*"/>
</authorization>
</system.web>
</location>
<location path= "Sectionone"
<system.web>
<authorization>
<allow users= "Sectionone" roles= "Administrators"
<deny users= "*"/>
</authorization>
</system.web>
</location>
<location path= "Sectiontwo.aspx"
<system.web>
<authorization>
<allow users= "Sectiontwo" roles= "Administrators"
<deny users= "*"/>
</authorization>
</system.web>
</location>
<location path= "Sectiontwo"
<system.web>
<authorization>
<allow users= "Sectiontwo" roles= "Administrators"
<deny users= "*"/>
</authorization>
</system.web>
Localize Site Map Data
The navigation data stored in the sitemap file may need to be localized (localize). The URL, title, and description properties in the element can also be localized. In addition, any custom attributes that developers place in the element can also be localized.
The following example contains localized text in English and French. Its Web.sitemap file uses two types of localized expressions (implicit and explicit) to implement this functionality. The sitemap file uses enablelocalization=true in the root sitemap element to indicate that it uses localized data.
The implicit expression of the Sitemap file allows developers to easily mark each element with a lookup key (lookup key), which is used to retrieve resources from a resource file. In the example Web.sitemap, all nodes have an implicit resource expression in addition to the first node. Its syntax resembles resourcekey= "autos". When XmlSiteMapProvider retrieves SiteMapNode according to the information in the Web.sitemap file, it is based on the name of the SiteMapNode property, ResourceKey and retrieves the string resource for the value of the siteMapFile property that is configured for the provider. When using the "Autos" node in the example, the provider (provider) finds resource files that begin with "Web.sitemap" based on the current culture. This means that for a browser that sends French header information, the provider looks for a resource file named Web.sitemap.fr.resx. In this resource file, the provider finds the resource key based on the ResourceKey + "." + [SiteMapNode property name]. For example, the title attribute of the "autos" node is used as an example, and the provider looks for a resource in the Web.sitemap.fr.resx resource file that has the key as Autos.title.
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
>
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 a 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.
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 Br>dim typeidurlencoded 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
URL mapping
The URL mapping attribute uses the configuration information stored in Web.config to remap the received request (remap) to a different URL. The remapping occurs before all other processing operations on the request. The following example shows the remapping of a page request, in which any file type can actually remap the request to a different URL.
To define a remapping URL
The configuration information for URL mappings is stored in web.config. Each element that is nested in the defines a rule for remapping the (inbound) URL into the site. The URL property defines the exact (as-is) attribute of the URL that enters the site, and the URL mapping attribute attempts to match it. If a exact matching operation occurs, the Mappedurl property value is rewritten to the URL that enters the site. Note that this feature does not support more advanced rules (such as matching based on wildcard wildcards expressions expressions).
Example Web.config defines a mapping rule for a large number of URLs. The example used by the Web.sitemap file defines a large number of nodes with URL values that will be mapped. The result is a combination of URL mapping and site navigation that allows developers to define the navigation structure with a friendly URL and use URL mapping to write the request back to a different page to perform the actual process.
When you run the example, note how the menu and TreeView controls display navigation data based on the site structure defined in the Web.sitemap file. If you hover your mouse over a link in the TreeView control or the SiteMapPath control in the upper-right corner, the URL displayed in the status bar is a friendly URL link. When you click on any navigation link, the actual running page is default.aspx. However, navigation information in the menu, TreeView, and SiteMapPath controls is still reflected as a friendly URL structure.
At the bottom of the page you can also see the values returned by Request.path, request.querystring["category", and Request.rawurl. The value returned by Request.path and request.querystring["category" always reflects the result of remapping the URL into the site. However, the Request.rawurl value reflects the friendly URL before the remapping. When the site navigation feature attempts to match the URL information with the data contained in the sitemap file, it uses Request.rawurl. If the matching value is not found, XmlSiteMapProvider the Request.path as a substitute. In the example, all friendly URLs have entries in the Web.sitemap file, so controls that use site navigation always display and reference nodes based on friendly URLs.
>