如何:本地化站点地图数据

更新:2007 年 11 月

您可以本地化站点地图中的以下属性:

这些属性是通过使用显式或隐式表达式作为属性值进行本地化的。有关表达式的更多信息,请参见 ASP.NET 表达式概述。有关资源文件的信息,请参见 ASP.NET 应用程序中的资源

通过使用显式表达式来本地化站点地图节点的 Title 或 Description 属性

  1. 在站点地图中,将 EnableLocalization 属性设置为 true。例如,在 Web.sitemap 文件中,将 <siteMap> 节点更改为类似下面的代码:

    <siteMap enableLocalization="true">
    
  2. 在站点地图中,将要本地化的属性的值更改为资源字符串,如下面的区分大小写的代码行所示:

    $resources:ClassName,KeyName,DefaultValue
    

    例如,在 Web.sitemap 文件中,您的站点地图节点可能类似于下面的代码示例。

    <siteMapNode 
      url="~/Home.aspx" 
      title="$resources:SiteMapLocalizations,HomePageTitle"
    description="$resources:SiteMapLocalizations,HomePageDescription,Default description"
      myCustomAttribute="$resources: CustomLocalizations,MyCustomAttribute"
    />
    

    本地化的标题和说明是通过使用 HomePageTitle 和 HomePageDescription 资源键,从名为 SiteMapLocalizations.resx 的文件中获取的。本地化的自定义属性是从名为 CustomLocalizations.resx 的单独的文件中获取的。有关资源字符串的更多信息,请参见 ASP.NET 表达式概述

  3. 在应用程序的根目录中名为 App_GlobalResources 的文件夹中创建 .resx 文件。在 .resx 文件中,各个属性值根据键名的值进行索引。对于前面的示例,SiteMapLocalizations.resx 文件可能类似如下所示。

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <xsd:schema id="root"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:element name="root" msdata:IsDataSet="true">
          <xsd:complexType>
            <xsd:choice maxOccurs="unbounded">
              <xsd:element name="metadata">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" />
                  <xsd:attribute name="type" type="xsd:string" />
                  <xsd:attribute name="mimetype" type="xsd:string" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="assembly">
                <xsd:complexType>
                  <xsd:attribute name="alias" type="xsd:string" />
                  <xsd:attribute name="name" type="xsd:string" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="data">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                    <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
                  <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
                  <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="resheader">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:choice>
          </xsd:complexType>
        </xsd:element>
      </xsd:schema>
      <resheader name="resmimetype">
        <value>text/microsoft-resx</value>
      </resheader>
      <resheader name="version">
        <value>2.0</value>
      </resheader>
      <resheader name="reader">
        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
      </resheader>
      <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
      </resheader>
      <data name="HomePageTitle">
        <value xml:space="preserve">Home Page</value>
      </data>
      <data name="HomePageDescription">
        <value xml:space="preserve">Home page of site</value>
      </data>
    </root>
    
  4. 如果需要将字符串本地化为多种语言,可以创建其他 .resx 文件,并在文件名中包含区域设置信息。例如,资源文件的法语版本可命名为 SiteMapLocalizations.fr.resx。

通过使用隐式表达式来本地化站点地图节点的 Title 或 Description 属性

  1. 在站点地图中,将 EnableLocalization 属性设置为 true。例如,在 Web.sitemap 文件中,将 <siteMap> 节点更改为类似下面的代码:

    <siteMap enableLocalization="true">
    
  2. 在站点地图中,使用下面的一行区分大小写的代码将名为 resourceKey 的属性添加到想要进行本地化的站点地图节点:

    resourceKey="HomePage"
    

    例如,在 Web.sitemap 文件中,<siteMap> 节点可能类似于下面的代码:

    <siteMapNode 
      url="~/Home.aspx" 
      resourceKey="HomePage"
      title = "Default Title"   
      description = "Default Description" 
     />
    

    本地化的标题和说明是通过使用 HomePage.title 和 HomePage.description 资源键,从名为 Web.sitemap.resx 的文件中获取的。

  3. 在应用程序的根目录中名为 App_GlobalResources 的文件夹中创建 .resx 文件。在 .resx 文件内,各个属性值根据名称进行索引。对于前面的示例,Web.sitemap.resx 文件可能类似于以下内容。

    ms178427.alert_note(zh-cn,VS.90).gif说明:

    全局资源文件中的键名不应包含句点 (.)。但是,当使用隐式表达式时,在站点地图文件中引用的全局资源中需要句点。这是因为 resourceKey 语法的缘故。在 Visual Web Developer 等编辑环境中,如果在键名中使用句点,您可能会收到设计时错误。但是,该错误不会影响您编辑或保存文件,可以忽略它。

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <xsd:schema id="root"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:element name="root" msdata:IsDataSet="true">
          <xsd:complexType>
            <xsd:choice maxOccurs="unbounded">
              <xsd:element name="metadata">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" />
                  <xsd:attribute name="type" type="xsd:string" />
                  <xsd:attribute name="mimetype" type="xsd:string" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="assembly">
                <xsd:complexType>
                  <xsd:attribute name="alias" type="xsd:string" />
                  <xsd:attribute name="name" type="xsd:string" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="data">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                    <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
                  <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
                  <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="resheader">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:choice>
          </xsd:complexType>
        </xsd:element>
      </xsd:schema>
      <resheader name="resmimetype">
        <value>text/microsoft-resx</value>
      </resheader>
      <resheader name="version">
        <value>2.0</value>
      </resheader>
      <resheader name="reader">
        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
      </resheader>
      <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
      </resheader>
      <data name="HomePage.description">
        <value xml:space="preserve">Home Page Description</value>
      </data>
      <data name="HomePage.Text">
        <value xml:space="preserve">Home Page Text from Resource File</value>
      </data>
      <data name="HomePage.title">
        <value xml:space="preserve">Home Page Title from Resource File</value>
      </data>
      <assembly alias="mscorlib" name="mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <data name="LabelResource1.Visible" type="System.Boolean, mscorlib">
        <value>True</value>
      </data>
    </root>
    
  4. 如果需要将字符串本地化为多种语言,可以创建其他 .resx 文件,并在文件名中包含区域设置信息。例如,资源文件的法语版本可命名为 Web.sitemap.fr.resx。

本地化站点地图中的 URL 属性

在站点地图中,Url 属性的本地化方式不能与 TitleDescription 属性相同。

基于用户的区域设置定义不同的导航结构

  1. 为每个区域设置定义不同的站点地图文件。

  2. 将每个站点地图添加到 Web.config 文件中。有关更多信息,请参见如何:配置多个站点地图和站点地图提供程序

  3. 在运行时以编程方式切换到适当的 Provider。这是通过将 SiteMapDataSource 对象的 SiteMapProvider 属性或 SiteMapPath 对象的 SiteMapProvider 属性设置为提供程序的名称来完成的。有关更多信息,请参见如何:以编程方式修改内存中的站点地图节点

可靠编程

同一站点地图文件中不能同时使用显式和隐式表达式。

请参见

任务

如何:配置多个站点地图和站点地图提供程序

如何:以编程方式修改内存中的站点地图节点

概念

ASP.NET 站点地图

ASP.NET 应用程序中的资源

ASP.NET 表达式概述

保证 ASP.NET 站点导航的安全

保证数据访问的安全

参考

Title

Description

Attributes

其他资源

寄宿环境中的 ASP.NET 应用程序安全性