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檔案,指定包含 ZipCodestring 別之 屬性的使用者設定檔,以及 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 類別,以建立自訂設定檔。 自訂設定檔的類型是在應用程式的 Web.config 檔案中設定檔組態專案的 屬性中指定 inherits

重要

此範例包含一個文字方塊,可接受使用者輸入,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

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 會建立 類型的新類別 ProfileCommon ,其繼承自 ProfileBase 類別。 強型別存取子會針對設定檔組態區段中定義的每個屬性新增至 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)

適用於

另請參閱