ProfileBase 类

定义

提供对配置文件属性值和信息的非类型化访问。

public ref class ProfileBase : System::Configuration::SettingsBase
public class ProfileBase : System.Configuration.SettingsBase
type ProfileBase = class
    inherit SettingsBase
Public Class ProfileBase
Inherits SettingsBase
继承
ProfileBase
派生

示例

下面的代码示例演示一个Web.config文件,该文件指定包含 ZipCode 类型的 string 属性和 RecentSearchList 类型的 StringCollection属性的用户配置文件。 当前 HttpContext 生成的Profile属性将具有每个指定属性的强类型访问器。

<configuration>
  <system.web>
    <anonymousIdentification enabled="true" />

    <profile enabled="true" defaultProvider="SqlProvider" >
      <properties>
        <add name="ZipCode" allowAnonymous="true" />
        <add name="RecentSearchList"
          type="System.Collections.Specialized.StringCollection"
          serializeAs="Xml"
          allowAnonymous="true" />
      </properties>
    </profile>
  </system.web>
</configuration>

以下示例演示一个 ASP.NET 页,该页读取并设置 ZipCode 为用户配置文件指定的 属性。 在尝试运行此代码之前,请在网站的 ASP.NET 配置设置中将提供程序设置为默认值 AspNetSqlProvider

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Page_PreRender()
{
  if (Profile.ZipCode == null)
  {
    PersonalizePanel.Visible = false;
    GetZipCodePanel.Visible = true;
  }
  else
  {
    ZipCodeLabel.Text = Profile.ZipCode;

    // Get personalized information for zip code here.

    PersonalizePanel.Visible = true;
    GetZipCodePanel.Visible = false;
  }
}

public void ChangeZipCode_OnClick(object sender, EventArgs args)
{
  ZipCodeTextBox.Text = Profile.ZipCode;
  Profile.ZipCode = null;

  PersonalizePanel.Visible = false;
  GetZipCodePanel.Visible = true;
}

public void EnterZipCode_OnClick(object sender, EventArgs args)
{
  Profile.ZipCode = ZipCodeTextBox.Text;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Page_PreRender()

  If Profile.ZipCode = Nothing Then
    PersonalizePanel.Visible = False
    GetZipCodePanel.Visible = True
  Else
    ZipCodeLabel.Text = Profile.ZipCode

    ' Get personalized information for zip code here.

    PersonalizePanel.Visible = True
    GetZipCodePanel.Visible = False
  End If

End Sub

Public Sub ChangeZipCode_OnClick(sender As Object, args As EventArgs)
  ZipCodeTextBox.Text = Profile.ZipCode
  Profile.ZipCode = Nothing

  PersonalizePanel.Visible = False
  GetZipCodePanel.Visible = True
End Sub

Public Sub EnterZipCode_OnClick(sender As Object, args As EventArgs)
  Profile.ZipCode = ZipCodeTextBox.Text
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>

下面的代码示例定义一个类,该类继承自 ProfileBase 类以创建自定义配置文件。 自定义配置文件的类型在 inherits 应用程序的 Web.config 文件中 配置文件 配置元素的 属性中指定。

重要

此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

using System;
using System.Web.Profile;

namespace Samples.AspNet.Profile
{
  public class EmployeeProfile : ProfileBase
  {
    [SettingsAllowAnonymous(false)]
    [ProfileProvider("EmployeeInfoProvider")]
    public string Department
    {
      get { return base["EmployeeDepartment"].ToString(); }
      set { base["EmployeeDepartment"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    [ProfileProvider("EmployeeInfoProvider")]
    public EmployeeInfo Details
    {
      get { return (EmployeeInfo)base["EmployeeInfo"]; }
      set { base["EmployeeInfo"] = value; }
    }
  }

  public class EmployeeInfo
  {
    public string Name;
    public string Address;
    public string Phone;
    public string EmergencyContactName;
    public string EmergencyContactAddress;
    public string EmergencyContactPhone;
  }
}
Imports System.Web.Profile

Namespace Samples.AspNet.Profile

  Public Class EmployeeProfile
    Inherits ProfileBase

    <SettingsAllowAnonymous(False)> _
    <ProfileProvider("EmployeeInfoProvider")> _
    Public Property Department As String
      Get
        Return MyBase.Item("EmployeeDepartment").ToString()
      End Get
      Set
        MyBase.Item("EmployeeDepartment") = value
      End Set
    End Property

    <SettingsAllowAnonymous(False)> _
    <ProfileProvider("EmployeeInfoProvider")> _
    Public Property Details As EmployeeInfo
      Get
        Return CType(MyBase.Item("EmployeeInfo"), EmployeeInfo)
      End Get
      Set
        MyBase.Item("EmployeeInfo") = value
      End Set
    End Property
  End Class

  Public Class EmployeeInfo
    Public Name As String
    Public Address As String
    Public Phone As String
    Public EmergencyContactName As String
    Public EmergencyContactAddress As String
    Public EmergencyContactPhone As String
  End Class

End Namespace

注解

ASP.NET 使用 ProfileBase 类创建用于用户配置文件的类。 启动启用了用户配置文件的应用程序时,ASP.NET 会创建继承自 ProfileBase 类的 类型ProfileCommon的新类。 强类型访问器将添加到ProfileCommon配置文件配置节中定义的每个属性的 类中。 类的ProfileCommon强类型访问器调用GetPropertyValue基类的 ProfileBaseSetPropertyValue 方法,以分别检索和设置配置文件属性值。 类的 ProfileCommon 实例设置为 Profile ASP.NET 应用程序的 属性值。

若要在 ASP.NET 应用程序中创建用户配置文件的实例,建议使用 Create 方法。

继承者说明

可以创建继承自抽象类的自定义配置文件实现, ProfileBase 并为 配置文件配置元素 中未指定的用户配置文件定义属性。 可以使用配置文件配置元素的 属性在 web.config 文件中inherits指定自定义用户配置文件类型,如以下示例所示。 类的代码 EmployeeProfile 包含在本主题的“示例”部分中。

<configuration>
   <system.web>
      <profile inherits="Samples.AspNet.Profile.EmployeeProfile"  
      defaultProvider="SqlProvider">  
      <providers>  
         <clear />  
         <add  
            name="SqlProvider"  
            type="System.Web.Profile.SqlProfileProvider"   
            connectionStringName="SqlServices"   
            description="SQL Profile Provider for Sample"/>   
         <add  
            name="EmployeeInfoProvider"  
            type="System.Web.Profile.SqlProfileProvider"   
            connectionStringName="SqlServices"   
            description="SQL Profile Provider for Employee Info"/>   
      </providers>  
      
      <properties>  
         <add name="GarmentSize" />  
      </properties>  
      </profile>  
   </system.web>
</configuration>

构造函数

ProfileBase()

创建 ProfileBase 类的实例。

属性

Context

获取关联的设置上下文。

(继承自 SettingsBase)
IsAnonymous

获取一个指示用户配置文件是否用于匿名用户的值。

IsDirty

获取一个值,该值表明是否修改了任何配置文件属性。

IsSynchronized

获取一个值,该值指示访问对象是否同步(线程安全)。

(继承自 SettingsBase)
Item[String]

获取或设置按属性名进行索引的配置文件属性值。

LastActivityDate

获取最近一次读取或修改配置文件的日期和时间。

LastUpdatedDate

获取最近一次修改配置文件的日期和时间。

Properties

获取配置文件中每个属性的 SettingsProperty 对象的集合。

PropertyValues

获取设置属性值的集合。

(继承自 SettingsBase)
Providers

获取设置提供程序的集合。

(继承自 SettingsBase)
UserName

获取该配置文件的用户名。

方法

Create(String)

ASP.NET 使用它为指定的用户名创建配置文件的一个实例。

Create(String, Boolean)

ASP.NET 使用它为指定的用户名创建配置文件的一个实例。 带有一个参数,用来指示用户是经过身份验证的用户还是匿名用户。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetProfileGroup(String)

获取按组名标识的属性组。

GetPropertyValue(String)

获取配置文件属性的值。

GetType()

获取当前实例的 Type

(继承自 Object)
Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection)

初始化 SettingsBase 对象使用的内部属性。

(继承自 SettingsBase)
Initialize(String, Boolean)

初始化当前用户的配置文件属性值和信息。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Save()

用已更改的配置文件属性值更新配置文件数据源。

SetPropertyValue(String, Object)

设置配置文件属性的值。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅