Tuesday 11 March 2014

What web.sitemap is and what the advantages in ASP.NET

In this article I will explain what web.sitemap is and what the advantages in ASP.NET

In ASP.NET the menu can be stored in a file to make it easier to maintain. This file is normally called web.sitemap, and is stored in the root directory of the web. It’s very useful for Page navigation.

Asp.Net menu control displays menu statically or dynamically using Sitemap Data Source control, which using site map control in ASP.Net. Here I am going to explain how to display menu using sitemap control.

A site map is made up of a series of related SiteMapNode objects. The SiteMapNodes are related in such a way as to form a hierarchy. The hierarchy contains a single root node, which is the sole node in the hierarchy that does not have a parent node. Each node in the hierarchy represents a logical section of the website. Each section can have a title, URL, description, and so on, which are modeled by the SiteMapNodes class's properties.Below is the design code for web.sitemap file.


<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode title="Home" url="~/Home.aspx">
    <siteMapNode     title="Admin Masters" description="">
      <siteMapNode url="~/ Country.aspx" title="Country Master"  description="" />
      <siteMapNode url="~/ State.aspx" title="State Master"  description="" />
      <siteMapNode url="~/ District.aspx" title="District Master"  description="" />
      <siteMapNode url="~/ City.aspx" title="City Master"  description="" />
    </siteMapNode>  
    <siteMapNode title="Reports" description="">
      <siteMapNode url="~/CountryWIse.aspx" title=" CountryWIse "  description="" />
      <siteMapNode url="~/StateWise.aspx" title=" StateWise "  description=" StateWise " />
      <siteMapNode url="~/DistrictWise.aspx" title=" DistrictWise "  description="" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

Create a master page in our website by right clicking the website in the solution explorer, choosing Add NewItem, and then selecting MasterPage icon. I'm going to link some aspx pages through our menu. So now we have to create some .aspx pages as a content page of masterpage. We can create aspx pages by right clicking the masterpage and choose the add content page, then rename it. Here we need to add Menu Tag and CSS to the menu tag and also add DataSource to the menu tag.

<asp:Menu ID="Menu1" runat="server" RenderingMode="List" Orientation="Horizontal"
                                    StaticEnableDefaultPopOutImage="False" Width="100%"
                                    StaticDisplayLevels="2" CssClass="Menu"
                                    BackColor="#80a2d0" DataSourceID="SiteMapDataSource1"
                                    >
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

A valid site-map file contains only one siteMapNode element that is located immediately under the siteMap element. But the first-level siteMapNode element can contain any number of child siteMapNode elements. Additionally, a valid sitemap file must not have duplicate URLs, though the url attributes can be empty. Providers other than the default site-map provider for ASP.NET might not have this restriction.

No comments:

Post a Comment