Professional ASP.NET 3.5 in C# and Visual Basic Part 76 doc

10 295 0
Professional ASP.NET 3.5 in C# and Visual Basic Part 76 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

Evjen c14.tex V2 - 01/28/2008 2:39pm Page 707 Chapter 14: Site Navigation Listing 14-28: Working with the CurrentNode object VB <%@ Page Language="VB" %> <script runat="server" language="vb"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = SiteMap.CurrentNode.Description & "<br>" & _ SiteMap.CurrentNode.HasChildNodes & "<br>" & _ SiteMap.CurrentNode.NextSibling.ToString() & "<br>" & _ SiteMap.CurrentNode.ParentNode.ToString() & "<br>" & _ SiteMap.CurrentNode.PreviousSibling.ToString() & "<br>" & _ SiteMap.CurrentNode.RootNode.ToString() & "<br>" & _ SiteMap.CurrentNode.Title & "<br>" & _ SiteMap.CurrentNode.Url End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>SiteMapDataSource</title> </head> <body> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server"></asp:Label> </form> </body> </html> C# <%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, System.EventArgs e) { Label1.Text = SiteMap.CurrentNode.Description + "<br>" + SiteMap.CurrentNode.HasChildNodes + "<br>" + SiteMap.CurrentNode.NextSibling.ToString() + "<br>" + SiteMap.CurrentNode.ParentNode.ToString() + "<br>" + SiteMap.CurrentNode.PreviousSibling.ToString() + "<br>" + SiteMap.CurrentNode.RootNode.ToString() + "<br>" + SiteMap.CurrentNode.Title + "<br>" + SiteMap.CurrentNode.Url; } </script> As you can see from this little bit of code, by using the SiteMap class and the CurrentNode object you can work with a plethora of information regarding the current page. Running this page, you get the following results printe d to the screen: The Latest Market Information True Funds Finance 707 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 708 Chapter 14: Site Navigation Quotes Home Markets /SiteNavigation/Markets.aspx Using the CurrentNode property, you can actually create your own style of the SiteMapPath control, as illustrated in Listing 14-29. Listing 14-29: Creating a custom navigation display using the CurrentNode property VB <%@ Page Language="VB" %> <script runat="server" language="vb"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Hyperlink1.Text = SiteMap.CurrentNode.ParentNode.ToString() Hyperlink1.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url Hyperlink2.Text = SiteMap.CurrentNode.PreviousSibling.ToString() Hyperlink2.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url Hyperlink3.Text = SiteMap.CurrentNode.NextSibling.ToString() Hyperlink3.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>SiteMapDataSource</title> </head> <body> <form id="form1" runat="server"> Move Up: <asp:Hyperlink ID="Hyperlink1" runat="server"></asp:Hyperlink><br /> < <asp:Hyperlink ID="Hyperlink2" runat="server"></asp:Hyperlink> | <asp:Hyperlink ID="Hyperlink3" runat="server"></asp:Hyperlink> > </form> </body> </html> C# <%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, System.EventArgs e) { Hyperlink1.Text = SiteMap.CurrentNode.ParentNode.ToString(); Hyperlink1.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url; Hyperlink2.Text = SiteMap.CurrentNode.PreviousSibling.ToString(); Hyperlink2.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url; Hyperlink3.Text = SiteMap.CurrentNode.NextSibling.ToString(); Hyperlink3.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url; } </script> 708 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 709 Chapter 14: Site Navigation When run, this page gives you your own custom navigation structure, as shown in Figure 14-30. Figure 14-30 URL Mapping The URLs used by Web pages can sometimes get rather complex as your application grows and grows. Sometimes, you could be presenting Web pages that change their content based on querystrings that are provided via the URL, such as: http://www.asp.net/forums/view.aspx?forumid=12&categoryid=6 In other cases, your Web page might b e so deep within a hierarchy of folders t hat the URL has become rather cumbersome for an end user to type or remember when they want to pull up the page later in their browser. There are also moments when you want a collection of pages to look like they are the same page or a single destination. In cases such as these, you can take advantage of a ASP.NET feature called URL mapping. URL mapping enables you to map complex URLs to simpler ones. You accomplish this through settings you apply in the web.config file using the < urlMappings > element (see Listing 14-30). Listing 14-30: Mapping URLs using the <urlMappings> element <configuration> <system.web> <urlMappings> <add url="~/Content.aspx" mappedUrl="~/SystemNews.aspx?categoryid=5" /> </urlMappings> </system.web> </configuration> 709 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 710 Chapter 14: Site Navigation In this example, we provide a fake URL — Content.aspx — that is mapped to a more complicated URL: SystemNews.aspx?categoryid=5 . With this construction in place, when the end user types URL Con- tent.aspx , the application knows to invoke the more complicated URL SystemNews.aspx?categoryid=5 page. This takes place without the URL even being changed in the browser. Even after the page has com- pletely loaded, the browser will still show the Content.aspx page as the destination — thereby tricking the end user in a sense. It is important to note that in this situation, the end user is routed to SystemNews.aspx?categoryid=5 no matter what — even if a Content.aspx page exists! Therefore, it is important to map to pages that are not actually contained within your application. Sitemap Localization The improved resource files ( .resx ) are a great way to localize ASP.NET applications. This localization of Web applications using ASP.NET is covered in Chapter 31 o f this book. However, this introduction focused on applying localization features to the pages of your applications; we didn’t demonstrate how to take t his localization capability f urther by applying it to items such as the Web.sitemap file. Structuring the Web.sitemap File for Localization Just as it is possible to apply localization instructions to the pages of your ASP.NET Web applications, you can also use the same framework to accomplish your localization tasks in the Web.sitemap file. To show you this in action, Listing 14-31 constructs a Web.sitemap file somewhat similar to the one presented in Listing 14-1, but much simpler. Listing 14-31: Creating a basic .sitemap file for localization <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" enableLocalization="true"> <siteMapNode url="Default.aspx" resourceKey="Home"> <siteMapNode url="News.aspx" resourceKey="News"> <siteMapNode url="News.aspx?cat=us" resourceKey="NewsUS" /> <siteMapNode url="News.aspx?cat=world" resourceKey="NewsWorld" /> <siteMapNode url="News.aspx?cat=tech" resourceKey="NewsTech" /> <siteMapNode url="News.aspx?cat=sport" resourceKey="NewsSport" /> </siteMapNode> </siteMapNode> </siteMap> Looking at Listing 14-31, you can see that we have a rather simple Web.sitemap file. To enable the localization capability from the Web.sitemap file, you have to turn this capability on by using the enable- Localization attribute in the < siteMap > element and setting it to true . Once enabled, you can then define each of the navigation nodes as you would normally, using the < siteMapNode > element. In this case, however, b ecause you are going to define the contents of these navigation pieces (most notably the title and description attributes) in various .resx files, there is no need to repeatedly define these items in this file. That means you need to define only the url attribute for this example. It is important to note, however, that you could also define this attribute through your .resx files, thereby forwarding end users to different pages depending on their defined culture settings. 710 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 711 Chapter 14: Site Navigation The next attribute to note is the resourceKey attribute used in the < siteMapNode > elements. This is the key that is used and defined in the various .resx files you will implement. Take t he following < siteMapNode > element as an example: <siteMapNode url="News.aspx" resourceKey="News"> </siteMapNode> In this case, the value of the resourceKey (and the key that will be used in the .resx file) is News .This means that you are then able to define the values of the title and description attributes in the .resx file using the following syntax: News.Title News.Description Now that the Web.sitemap is in place, the next step is to make some minor modifications to the Web.config file, as shown next. Making Modifications to the Web.config File Now that the Web.sitemap file is in place and ready, the next step is to provide some minor additions to the Web.config file. In order for your Web application to make an automatic detection of the culture of the users visiting the various pages you are providing, you need to set the Culture and UICulture settings in the @Page directive, or set these attributes for automatic detection in the < globalization > element of the Web.config file. When you are working with navigation and the Web.sitemap file, as we are, it is actually best to make this change in the Web.config file so that it automatically takes effect on each and every page in your application. This makes it much simpler because you won’t have to make these additions yourself to each and every page. To make these changes, open your Web.config file and add a < globalization > element, as shown in Listing 14-32. Listing 14-32: Adding culture detection to the Web.config file <configuration> <system.web> <globalization culture="auto" uiCulture="auto" /> </system.web> </configuration> For the auto-detection capabilities to occur, you simply need to set the culture and uiCulture attributes to auto . You could have also defined the values as auto:en-US , which means that the automatic culture detection capabilities should occur, but if the culture defined is not found in the various resource files, then use en-US (American English) as the default culture. However, because we are going to define a default Web.sitemap set of values, there really is no need for you to bring forward this construction. Next, you need to create the assembly resources files that define the values used by the Web.sitemap file. 711 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 712 Chapter 14: Site Navigation Creating Assembly Resource (.resx) Files To create a set of assembly resource files that you will use with the Web.sitemap file, create a folder in your project called App_GlobalResources . If you are using Visual Studio 2008 or Visual Web Developer, you can add this folder by right-clicking on the project and selecting Add Folder ➪ App_- GlobalResources. After the folder is in place, the next step is to add two assembly resource files to this folder. Name the first file Web.sitemap.resx and the second one Web.sitemap.fi.resx . Your goal with these two files is to have a default set of values for the Web.sitemap file that will be defined in the Web.sitemap.resx file, and a version of these values that has been translated to the Finnish language and is contained in the Web.sitemap.fi.resx file. The fi value used in the name will be the file used by individuals who have their preferred language set to fi-FI . Other variations of these constructions are shown in the following table. .resx File Culture Served Web.sitemap.resx The default values used when the end user’s culture cannot be identified through another .resx file Web.sitemap.en.resx The resource file used for all en (English) users Web.sitemap.en-gb.resx The resource file used for t he English speakers of Great Britain Web.sitemap.fr-ca.resx The resource file used for the French speakers of Canada Web.sitemap.ru.resx The resource file used for Russian speakers Now that the Web.sitemap.resx and Web.sitemap.fi.resx files are in place, the next step is to fill these files with values. To accomplish this task, you use the keys defined e arlier directly in the Web.sitemap file. Figure 14-31 shows the result of this exercise. Although the IDE states that these are not valid identifiers, the application still works with this model. After you have the files in place, you can test how this localization endeavor works, as shown in the following section. Testing the Results Create a page in your application and place a TreeView server control on the page. In addition to the TreeView control, you also have to include a SiteMapDataSource control to work with the Web.sitemap file you created. Be sure to tie the two controls together by giving the TreeView control the attribute DataSourceID="SiteMapDataSource1" , as demonstrated earlier in this chapter. If you have your language preference in Microsoft’s Internet Explorer set to en-us (American English), you will see the results shown in Figure 14-32. When you pull up the page in the browser, the culture of the request is checked. Because the only finely grained preference defined in the exampleisforusersusingthecultureof fi (Finnish), the default Web.sitemap.resx is use d instead. Be cause of this, the Web.sitemap.resx file is used to p opulate the 712 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 713 Chapter 14: Site Navigation values of the TreeView control, as shown in Figure 14-32. If the requestor has a culture setting of fi , however, he gets an entirely different set of results. Figure 14-31 Figure 14-32 To test this out, change the preferred language used in IE by selecting Tools ➪ Internet Options in IE. On the first tab (General), click the Languages button at the bottom of the dialog. You are presented with the Language Preferences dialog. Click the Add button and add the Finnish language setting to the list of options. The final step is to use the Move Up button to move the Finnish choice to the top of the list. In the end, you should see something similar to what is shown in Figure 14-33. 713 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 714 Chapter 14: Site Navigation Figure 14-33 With this setting in place, running the page with the TreeView control gives you the result shown in Figure 14-34. Figure 14-34 Now, when the page is requested, the culture is set to fi and correlates to the Web.sitemap.fi.resx file instead of to the default Web.sitemap.resx file. Security Trimming If you have been following the examples so far in this chapter, you might notice that one of the attributes available to a < siteMapNode > tag hasn’t yet been discussed. The roles attribute is a powerful one that 714 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 715 Chapter 14: Site Navigation allows you to provide an authorization model t o the items contained in the navigation system. This really means that you have the capability to display only the navigational items that a user is entitled to see and nothing more. The term commonly used for this behavior is security trimming. This section looks at how to apply security trimming to the application you a re building in ASP.NET 3.5. This ca pability is a good example of two ASP.NET 3.5 systems interacting with one another in the site navigation system. Security trimming works only when you have enabled the ASP.NET 3.5 role man- agement system. This system is covered in more detail in Chapter 16. Be sure to check out this chapter because this section does not go into much detail about this system. As an example of security trimming in your ASP.NET applications, this section shows you how to limit access to the navigation of your application’s administration system only to users who are contained within a specific application role. Setting Up Role Management for Administrators The first step is to set up your application to handle roles. This is actually a pretty simple process. One easy way to accomplish this task is to open the ASP.NET Web Site Administration Tool for your appli- cation and enable role management directly in this Web-based tool. You can get to this administration tool by clicking the ASP.NET Configuration button in the menu of the Solution Explorer in Visual Studio. This button has the logo of a hammer and a globe. After the ASP.NET Web Site Administration Tool is launched, select the Security tab; this brings you to a screen where you can administer the membership and role management systems for your application. First, you enable and build up the role management system, and then you also enable the membership system. The membership system is covered in detail in Chapter 16. After you turn on the membership system, you build some actual users in your application. You want a user to log in to your application and be assigned a specific role. This role assignment changes the site navigation system display. The Security tab in the ASP.NET Web Site Administration Tool is presented in Figure 14-35. On this page, you can easily enable the role management system by selecting the Enable roles link. After you have done this, you are informed that there are no roles in the system. To create the role that you need for the site navigation system, select the Create or Manage roles link. You are then presented with a page where you can create the administrator role. For this example, I named the role Admin. After adding the Admin role, click the Back button and then select the authentication type that is utilized for the application. You want to make sure that you have selected the From the internet option. This enables you then to create a user in the system. By default, these users a re stored in the Microsoft SQL Server Express Edition file that ASP.NET creates in your application. After you have selected the authen- tication type, you can then create the new user and place the user in the Admin role by making sure the role is selected (using a check box) on the screen where you are creating the user. 715 Evjen c14.tex V2 - 01/28/2008 2:39pm Page 716 Chapter 14: Site Navigation Figure 14-35 After you are satisfied that a user has been created and placed in the Admin role, you can check if the settings are appropriately set in the web.config file. This is presented in Listing 14-33. Listing 14-33: The role management system enabled in the web.config file <configuration> <system.web> <authentication mode="Forms" /> <roleManager enabled="true" /> </system.web> </configuration> Setting Up the Administrators’ Section The next step is to set up a page for administrators only. For this example, I named the page Admi- nOnly.aspx , and it contains only a simple string value welcoming administrators to the page. This page is locked down only for users who are contained in the Admin role. This is done by making the appropriate settings in the web.config file. This lockdown is shown in Listing 14-34. 716 . is a good example of two ASP. NET 3. 5 systems interacting with one another in the site navigation system. Security trimming works only when you have enabled the ASP. NET 3. 5 role man- agement system see and nothing more. The term commonly used for this behavior is security trimming. This section looks at how to apply security trimming to the application you a re building in ASP. NET 3. 5. This. similar to what is shown in Figure 14 -33 . 7 13 Evjen c14.tex V2 - 01/28/2008 2 :39 pm Page 714 Chapter 14: Site Navigation Figure 14 -33 With this setting in place, running the page with the TreeView

Ngày đăng: 05/07/2014, 19:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan