WEBSITE

ASP.NET 4 : Web Site Navigation (part 4) - Security Trimming, URL Mapping, URL Rewriting

3/27/2013 7:23:07 PM

6. Security Trimming

The ASP.NET navigation support works with the authentication and authorization mechanisms to support security trimming. Security trimming means showing only part of the menu based on the role of the current user. Of course, this means that the Web site must somehow authenticate the user. 

To make security trimming work, turn the securityTrimmingEnabled attribute on in web.config. The list of roles for which the navigation option is available is a property for each SiteMapNode.

7. URL Mapping

Finally, the ASP.NET navigation architecture supports URL mapping. URL mapping is mapping a virtual (or nonexistent) URL to an existing ASPX file in the web.config file using the urlMappings element. Setting up URL mappings causes ASP.NET to read the requested URL and uses the handler for the mapped URL. This is done in HttpApplication using HttpContext.RewritePath.

For example, imagine your Web site includes a single products page containing both CDs and DVDs. However, your UI model requires you to build a menu structure that separates the CD products and the DVD products into two options that appear separately on the menu. URL mapping provides a way of handling this situation.

Here's an exercise showing how to use URL mapping to represent a single page as two separate menu items. In this case, the page's content is distinguished by a URL parameter.

Implementing URL mapping

  1. Update the Products page so that it shows different content when the ID parameter is 1 or 2. This example divides the products into CDs and DVDs. The page displays different content based on the value of the ID parameter (whether it's 1 or 2 or something else). Place a Label control on the Products page and assign its ID property the value LabelProductType. Then, drop a ListBox on the page and assign its ID the value ListBoxProducts. The code-beside file then implements the URL mapping functionality in the Page_Load handler, as shown here:

    public partial class Products : System.Web.UI.Page
    {
        protected void AddCDsToListBox()
        {
            this.ListBoxProducts.Items.Add("CD- Snakes and Arrows");
            this.ListBoxProducts.Items.Add("CD- A Farewell To Kings");
            this.ListBoxProducts.Items.Add("CD- Moving Pictures");
            this.ListBoxProducts.Items.Add("CD- Hemispheres");
    
    
            this.ListBoxProducts.Items.Add("CD- Permanent Waves");
            this.ListBoxProducts.Items.Add("CD- Counterparts");
            this.ListBoxProducts.Items.Add("CD- Roll the Bones");
            this.ListBoxProducts.Items.Add("CD- Fly By Night");
            this.ListBoxProducts.Items.Add("CD- 2112");
        }
    
        protected void AddDVDsToListBox()
        {
            this.ListBoxProducts.Items.Add("DVD- A Show Of Hands");
            this.ListBoxProducts.Items.Add("DVD- Exit Stage Left");
            this.ListBoxProducts.Items.Add("DVD- Rush In Rio");
            this.ListBoxProducts.Items.Add("DVD- R30");
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.Params["ID"] == "1")
            {
                this.LabelProductType.Text = "CDs";
                AddCDsToListBox();
            }
            else if (this.Request.Params["ID"] == "2")
            {
                this.LabelProductType.Text = "DVDs";
                AddDVDsToListBox();
            }
            else
            {
                this.LabelProductType.Text = "All CDs and DVDs";
                AddCDsToListBox();
                AddDVDsToListBox();
            }
        }
    }
  2. Update the web.sitemap file to include the new menu items mapped to virtual files (for example, CDs.aspx and DVDs.aspx). Add this to the Web.site file:

    <?xml version="1.0" encoding="utf-8" ?>
      <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
        <siteMapNode url="~/Default.aspx" title="Home"
          description="This is the home page"
          ImageURL="~/homeimage.jpg">
        <siteMapNode url="~/Products.aspx" title="Products"
          description="This is the products page"
          ImageURL="~/productsimage.jpg">
          <siteMapNode url="~/CDs.aspx" title="CDs"
            description="This is the CDs page"
            ImageURL="~/productsimage.jpg"/>
          <siteMapNode url="~/DVDs.aspx" title="DVDs"
            description="This is the DVDs page"
            ImageURL="~/productsimage.jpg"/>
          </siteMapNode>
        <siteMapNode url="~/Support.aspx" title="Support"
          description="This is the support page"
          ImageURL="~/supportimage.jpg"/>
        <siteMapNode url="~/Contact.aspx" title="Contacts"
          description="This is the contacts page"
          ImageURL="~/contactimage.jpg">
        <siteMapNode url="~/ContactAddress/ContactAddress.aspx"
          title="Contact using physical address"
          description="This is the first contact page"
          ImageURL="~/contactPhysicalAddressimage.jpg"/>
        <siteMapNode url="~/ContactEmailPhone/ContactEmailPhone.aspx"
          title="Contact by email or phone"
          description="This is the second contact page"
          ImageURL="~/contactPhoneimage.jpg" />
        </siteMapNode>
      </siteMapNode>
    </siteMap>
  3. Add this to the web.config file:

    <configuration>
      <system.web>
        <urlMappings enabled="true">
          <add url="~/CDs.aspx" mappedUrl="~/Products.aspx?ID=1"/>
          <add url="~/DVDs.aspx" mappedUrl="~/Products.aspx?ID=2"/>
        </urlMappings>
      </system.web>
    </configuration>
  4. Run the page. Notice that changes occurred and two new items now appear on the Products menu. The site map points these two items to the CDs.aspx file and the DVDs.aspx file. Although the application does not include files with these names, users still see a page that works when they redirect using one of these menu items. The web.config file remaps the request back to the Products.aspx page, passing a URL parameter with a specific value. When the Products.aspx page is loaded and the ID parameter is 1 or 2, the page loads the list box with CD titles or DVD titles.

    The following graphic shows the CDs Products page being selected from the site map data:

    image with no caption

    The next graphic shows the DVDs Products page being selected from the site map data:

    image with no caption

    This graphic shows the main Products page being selected from the site map data:

    image with no caption

URL mapping is useful in all kinds of situations when you need to represent pages in a navigation control, even when there might not be a physical page to support it.

8. URL Rewriting

Microsoft Internet Information Services (IIS) 7.0 now includes a URL Rewrite Module that supports the more contemporary technique of URL rewriting. URL rewriting is a much more dynamic technique for redirecting requests than is the simple configuration file urlMappings technique used in the last example. For example, you can use URL rewriting to redirect based on various run-time criteria, such as server variables or HTTP headers. You can also set up redirects based on URL rewriting rules.

Other  
 
Top 10
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
Java Tutorials : Nested For Loop (part 1)
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS