1. Trang chủ
  2. » Công Nghệ Thông Tin

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

10 297 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 328,82 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

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 theSiteMapclass and theCurrentNodeobject you can work with a plethora of information regarding the current page Running this page, you get the following results printed to the screen:

The Latest Market Information

True

Funds

Finance

Trang 2

Home

Markets

/SiteNavigation/Markets.aspx

Using theCurrentNodeproperty, 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>

Trang 3

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 be so deep within a hierarchy of folders that 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

theweb.configfile 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>

Trang 4

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 URLSystemNews.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 theContent.aspxpage 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 toSystemNews.aspx?categoryid=5no

matter what — even if aContent.aspxpage 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 of this book However, this introduction

focused on applying localization features to the pages of your applications; we didn’t demonstrate how

to take this localization capability further by applying it to items such as theWeb.sitemapfile

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 theWeb.sitemapfile To show

you this in action, Listing 14-31 constructs aWeb.sitemapfile 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 simpleWeb.sitemapfile To enable the

localization capability from theWeb.sitemapfile, you have to turn this capability on by using the

enable-Localizationattribute in the<siteMap>element and setting it totrue Once enabled, you can then

define each of the navigation nodes as you would normally, using the<siteMapNode>element In this

case, however, because you are going to define the contents of these navigation pieces (most notably

thetitleanddescriptionattributes) in various.resxfiles, there is no need to repeatedly define these

items in this file That means you need to define only theurlattribute for this example It is important to

note, however, that you could also define this attribute through your.resxfiles, thereby forwarding end

users to different pages depending on their defined culture settings

Trang 5

The next attribute to note is theresourceKeyattribute used in the<siteMapNode>elements This

is the key that is used and defined in the various.resxfiles you will implement Take the following

<siteMapNode>element as an example:

<siteMapNode url="News.aspx" resourceKey="News">

</siteMapNode>

In this case, the value of theresourceKey(and the key that will be used in the.resxfile) isNews This

means that you are then able to define the values of thetitleanddescriptionattributes in the.resx

file using the following syntax:

News.Title

News.Description

Now that theWeb.sitemapis in place, the next step is to make some minor modifications to the

Web.configfile, as shown next

Making Modifications to the Web.config File

Now that theWeb.sitemapfile is in place and ready, the next step is to provide some minor additions

to theWeb.configfile 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 theCultureandUICulture

settings in the@Pagedirective, or set these attributes for automatic detection in the<globalization>

element of theWeb.configfile

When you are working with navigation and theWeb.sitemapfile, as we are, it is actually best to make

this change in theWeb.configfile 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 yourWeb.configfile 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 thecultureanduiCultureattributes

toauto You could have also defined the values asauto: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 useen-US(American English) as the default culture However, because we are going to define a

defaultWeb.sitemapset 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 theWeb.sitemapfile

Trang 6

Creating Assembly Resource (.resx) Files

To create a set of assembly resource files that you will use with theWeb.sitemapfile, create a folder

in your project calledApp_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 fileWeb.sitemap.resxand the second oneWeb.sitemap.fi.resx Your goal with these two files

is to have a default set of values for theWeb.sitemapfile that will be defined in theWeb.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.resxfile

Thefivalue used in the name will be the file used by individuals who have their preferred language set

tofi-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.resxfile

Web.sitemap.en.resx The resource file used for allen(English) users

Web.sitemap.en-gb.resx The resource file used for the 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 theWeb.sitemap.resxandWeb.sitemap.fi.resxfiles are in place, the next step is to fill these

files with values To accomplish this task, you use the keys defined earlier directly in theWeb.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 theWeb.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 toen-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 example is for users using the culture offi(Finnish), the default

Web.sitemap.resxis used instead Because of this, theWeb.sitemap.resxfile is used to populate the

Trang 7

values of the TreeView control, as shown in Figure 14-32 If the requestor has a culture setting offi,

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

Trang 8

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 tofiand correlates to theWeb.sitemap.fi.resxfile

instead of to the defaultWeb.sitemap.resxfile

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 Therolesattribute is a powerful one that

Trang 9

allows you to provide an authorization model to 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 are building in ASP.NET 3.5

This capability 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 are 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

Trang 10

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 theweb.configfile 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 theweb.configfile This lockdown is shown in Listing 14-34

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