注意:此类在 .NET Framework 2.0 版中是新增的。
提供对配置文件属性值和信息的非类型化访问。
命名空间:System.Web.Profile
程序集:System.Web(在 system.web.dll 中)
Public Class ProfileBase
Inherits SettingsBase
Dim instance As ProfileBase
public class ProfileBase : SettingsBase
public ref class ProfileBase : public SettingsBase
public class ProfileBase extends SettingsBase
public class ProfileBase extends SettingsBase
ASP.NET 使用 ProfileBase 类创建用于用户配置文件的类。在启动启用了用户配置文件的应用程序时,ASP.NET 会创建一个类型为 ProfileCommon 的新类,该类从 ProfileBase 类继承。强类型访问器被添加到 profile 配置节中为每个属性定义的 ProfileCommon 类中。ProfileCommon 类的强类型访问器调用 ProfileBase 基类的 GetPropertyValue 和 SetPropertyValue 方法,分别用于配置文件属性值的检索和设置。ProfileCommon 类的一个实例被设置为 ASP.NET 应用程序的 Profile 属性的值。
要在 ASP.NET 应用程序中创建用户配置文件的实例,建议您使用 Create 方法。
给继承者的说明
可以创建一个自定义配置文件实现,该实现从 ProfileBase 抽象类继承并为 profile 配置元素中未指定的用户配置文件定义相应的属性。您可以使用 profile 配置元素的 inherits 属性在 Web.config 文件中指定自定义的用户配置文件类型,如下面的示例所示。EmployeeProfile 类的代码包含在本主题的“示例”部分。
<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>
下面的代码示例显示这样一个 Web.config 文件:它指定一个用户配置文件,后者包含一个类型为 string 的 ZipCode 属性,以及一个类型为 StringCollection 的 RecentSearchList 属性。生成的当前 HttpContext 的 Profile 属性将拥有针对每个指定属性的强类型访问器。
<configuration>
<connectionStrings>
<add name="SqlServices" connectionString=
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
</connectionStrings>
<system.web>
<anonymousIdentification enabled="true" />
<profile enabled="true" defaultProvider="SqlProvider" >
<providers>
<add
name="SqlProvider"
connectionStringName="SqlServices"
applicationName="ProfileBaseApplication"
type="System.Web.Profile.SqlProfileProvider" />
</providers>
<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 属性。
<%@ Page Language="VB" %>
<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>
<head>
<title>Home Page</title>
</head>
<body>
<form 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="C#" %>
<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>
<head>
<title>Home Page</title>
</head>
<body>
<form 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 文件中的 profile 配置元素的 inherits 属性中指定的。
Imports System
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
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;
}
}
System.Object
System.Configuration.SettingsBase
System.Web.Profile.ProfileBase
System.Web.Profile.DefaultProfile
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0