ProfileBase 构造函数

定义

创建 ProfileBase 类的实例。Creates an instance of the ProfileBase class.

public:
 ProfileBase();
public ProfileBase ();
Public Sub New ()

例外

Web.config 文件的配置文件节中的 enabled 特性为 falseThe enabled attribute of the profile section of the Web.config file is false.

未能创建在 Web.config 文件的配置文件节中指定的属性类型。A property type specified in the profile section of the Web.config file could not be created.

- 或 --or-

将 Web.config 文件的配置文件节中某个属性的 allowAnonymous 特性设置为 true,并将 <anonymousIdentification> 元素的 enabled 特性设置为 falseThe allowAnonymous attribute for a property in the profile section of the Web.config file is set to true and the enabled attribute of the <anonymousIdentification> element is set to false.

- 或 --or-

将 Web.config 文件配置文件节中某个属性的 serializeAs 特性设置为 Binary,指定的 typeIsSerializable 属性将返回 falseThe serializeAs attribute for a property in the profile section of the Web.config file is set to Binary and the IsSerializable property of the specified type returns false.

- 或 --or-

未能在 Providers 集合中找到使用配置文件属性的 provider 特性指定的提供程序名称。The name of a provider specified using the provider attribute of a profile property could not be found in the Providers collection.

- 或 --or-

未能找到为配置文件属性指定的 typeThe type specified for a profile property could not be found.

- 或 --or-

使用与基类中的属性名称匹配的名称指定配置文件属性,基类在配置文件节的 inherits 特性中指定。A profile property was specified with a name that matches a property name on the base class specified in the inherits attribute of the profile section.

示例

以下 Web.config 文件指定了一个用户配置文件,其中包含类型为的 ZipCode 属性 stringRecentSearchList 类型为的属性 StringCollectionThe following Web.config file specifies a user profile that contains a ZipCode property of type string and a RecentSearchList property of type StringCollection. 当前生成的 Profile 属性 HttpContext 将具有每个指定属性的强类型访问器。The generated Profile property of the current HttpContext will have strongly typed accessors for each of the specified properties.

<configuration>
   <connectionStrings>
       <add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
   </connectionStrings>

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

   <profile 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 为用户配置文件指定的属性。The following ASP.NET page reads and sets the ZipCode property specified for the user profile.

重要

此示例包含一个文本框,该文本框接受用户输入,这是一个潜在的安全威胁。This example contains a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

<%@ 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>

注解

ASP.NET 使用 ProfileBase 类创建用于用户配置文件的类。ASP.NET uses the ProfileBase class to create the class used for the user profile. 当已启用用户配置文件的应用程序启动时,ASP.NET 将创建一个类型为的新类 ProfileCommon ,该类继承自 ProfileBase 类。When an application that has the user profile enabled is started, ASP.NET creates a new class of type ProfileCommon, which inherits from the ProfileBase class. 强类型访问器将添加到 ProfileCommon 配置文件 配置节中定义的每个属性的类。Strongly typed accessors are added to the ProfileCommon class for each property defined in the profile configuration section. 类的强类型访问器 ProfileCommon 调用基类的 GetPropertyValueSetPropertyValue 方法 ProfileBase ,分别检索和设置配置文件属性值。The strongly typed accessors of the ProfileCommon class call the GetPropertyValue and SetPropertyValue methods of the ProfileBase base class to retrieve and set profile property values, respectively. 类的实例 ProfileCommon 被设置为 Profile ASP.NET 应用程序的属性值。An instance of the ProfileCommon class is set as the value of the Profile property for the ASP.NET application.

备注

用于生成存储在属性中的类的基类 Profile 可以使用 inherits 配置文件的 配置 文件节的属性重写。The base class used to generate the class stored in the Profile property can be overridden using the inherits attribute of the profile section of the configuration file. 在这种情况下,您将指定从基类继承的自定义类 ProfileBaseIn this case you would specify a custom class that inherits from the ProfileBase base class.

此构造函数不应在应用程序代码中使用。This constructor is not intended to be used from application code. 若要创建用户配置文件的实例,请使用 Create 方法。To create an instance of a user profile, use the Create method.

适用于