1

我正在尝试按照此处的示例创建配置文件功能

让我绊倒的是我的 web.config 还包含group name对属性进行分组。我的 web.config 看起来像这样

网络配置:

    <profile defaultProvider="MyCMSSqlProfileProvider" automaticSaveEnabled="false" inherits ="TestProj.Controls.wProfile">
            <providers>
                <clear/>
                <add name="MyCMSSqlProfileProvider" connectionStringName="dbMyCMSConnectionString" applicationName="MyCMS" type="System.Web.Profile.SqlProfileProvider"/>
            </providers>
            <properties>
                 <group name="Personal">
                    <add name="FirstName" type="String" />
                    <add name="LastName" type="String" />
                    <add name="Gender" type="String" />
                    <add name="BirthDate" type="DateTime" />
                    <add name="Occupation" type="String" />
                    <add name="Website" type="String" />
                </group>
                <group name="Address">
                    <add name="Country" type="String" />
                    <add name="Address" type="String" />
                    <add name="AptNumber" type="String" />
                    <add name="City" type="String" />
                    <add name="State" type="String" />
                    <add name="PostalCode" type="String" />
                </group>

//.....etc

我开始将名为 ProfileInfo 的类更改为

配置文件信息.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestProj.controls
{
    [Serializable]
    public class Personal
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string BirthDate { get; set; }
        public string Occupation { get; set; }
        public string Website { get; set; }
    }

    [Serializable]
    public class Address
    {
        public string Country { get; set; }
        public string Address { get; set; }
        public string AptNumber { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
    }


}

但要知道这不会起作用,因为我需要将所有组名属性一起返回,通过wProfile

wProfile.cs:

using System.Web;
using System.Web.Profile;
namespace Project1.Account
{
    public class wProfile : ProfileBase
    {

        public ProfileInfo ProfileInfo
        { 
            get { return (ProfileInfo) GetPropertyValue("ProfileInfo"); }       
        } 

        public static wProfile GetProfile() 
        { 
            return (wProfile) HttpContext.Current.Profile; 
        } 

        public static wProfile GetProfile(string userName) 
        { 
            return (wProfile) Create(userName); 
        }  
    }
}

如何ProfileInfo.cs正确设置以公开所有属性(包括组名)的配置文件?

在代码隐藏中我想做这样的事情

        // Personal Info
        txtFirstName.Text = lprofile.Personal.FirstName;
        txtLastName.Text = lprofile.Personal.LastName;
        ddlGenders.SelectedValue = lprofile.Personal.Gender;
        if (lprofile.Personal.BirthDate != DateTime.MinValue)
            txtBirthDate.Text = lprofile.Personal.BirthDate.ToShortDateString();
        ddlOccupations.SelectedValue = lprofile.Personal.Occupation;
        txtWebsite.Text = lprofile.Personal.Website;

        // Address Info
        ddlCountries.SelectedValue = lprofile.Address.Country;
        txtAddress.Text = lprofile.Address.Address;
        txtAptNumber.Text = lprofile.Address.AptNumber;
        txtCity.Text = lprofile.Address.City;
        ddlStates1.SelectedValue = lprofile.Address.State;
        txtPostalCode.Text = lprofile.Address.PostalCode;
4

1 回答 1

1

您只需将Personal/Address作为属性添加到您的配置文件模型,即

[Serializable]
public class Personal
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public string BirthDate { get; set; }
    public string Occupation { get; set; }
    public string Website { get; set; }
}

[Serializable]
public class Address
{
    public string Country { get; set; }
    public string Address { get; set; }
    public string AptNumber { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

[Serializable]
public class ProfileInfo
{
    public Personal Personal { get; set; }
    public Address Address { get; set; }
}

文档

属性可以在用户配置文件中组织为属性组。配置文件属性组是使用组配置元素指定的。例如,可以将用户地址信息的不同属性组合在一个地址组中。然后,您可以使用组标识符和属性名称(例如 Profile.Address.Street 或 Profile.Address.City)访问分组的属性。

于 2013-08-21T12:34:18.003 回答