我不知道我缺少什么,但我在 Web.config 文件中添加了 Profile 属性,但无法访问 Profile。物品 在代码中或创建一个新的配置文件。

有帮助吗?

解决方案

我今天遇到了同样的问题,并且学到了很多东西。

Visual Studio中有两种项目 - <!>“Web站点项目<!>”;和<!>“Web应用程序项目。<!>”;出于对我来说完全神秘的原因,Web应用程序项目无法直接使用配置文件。 ...强类型类不会从Web.config文件中为您神奇地生成,因此您必须滚动你自己。

MSDN中的示例代码假定您正在使用网站项目,并且他们告诉您只需将<profile>部分添加到Web.config并使用Profile. 属性进行派对,但这在Web应用程序项目中不起作用。

您可以选择两种方式:

(1)使用 Web Profile Builder 。这是您添加到Visual Studio的自定义工具,可以从Web.config中的定义自动生成所需的Profile对象。

我选择不这样做,因为我不希望我的代码依赖于这个额外的工具进行编译,当他们试图构建我的代码而没有意识到他们需要时,这可能会给其他人造成问题。这个工具。

(2)创建自己的类,派生自ProfileBase以表示您的自定义配置文件。这比看起来容易。这是一个非常简单的例子,它添加了一个<!> quot; FullName <!> quot;字符串配置文件字段:

在您的web.config中:

<profile defaultProvider="SqlProvider" inherits="YourNamespace.AccountProfile">

<providers>
     <clear />
     <add name="SqlProvider"
          type="System.Web.Profile.SqlProfileProvider"
          connectionStringName="sqlServerMembership" />
</providers>

</profile>

在名为AccountProfile.cs的文件中:

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

namespace YourNamespace
{
    public class AccountProfile : ProfileBase
    {
        static public AccountProfile CurrentUser
        {
            get { return (AccountProfile)
                         (ProfileBase.Create(Membership.GetUser().UserName)); }
        }

        public string FullName
        {
            get { return ((string)(base["FullName"])); }
            set { base["FullName"] = value; Save(); }
        }

        // add additional properties here
    }
}

设置个人资料值:

AccountProfile.CurrentUser.FullName = "Snoopy";

获取个人资料值

string x = AccountProfile.CurrentUser.FullName;

其他提示

Web应用程序项目仍然可以使用ProfileCommon对象,但仅限于运行时。它的代码不是在项目本身生成的,而是由ASP.Net生成的类,并且在运行时存在。

获取对象的最简单方法是使用动态类型,如下所示。

在Web.config文件中声明配置文件属性:

<profile ...
 <properties>
   <add name="GivenName"/>
   <add name="Surname"/>
 </properties>

然后访问属性:

dynamic profile = ProfileBase.Create(Membership.GetUser().UserName);
string s = profile.GivenName;
profile.Surname = "Smith";

要保存对配置文件属性的更改:

profile.Save();

如果您习惯使用动态类型并且不介意缺少编译时检查和智能感知,则上述工作正常。

如果将此与ASP.Net MVC一起使用,如果将动态配置文件对象传递给视图,则必须执行一些额外的工作,因为HTML帮助程序方法与<!>“模型<!>不匹配;动态的对象。在将配置文件属性传递给HTML帮助程序方法之前,必须将它们分配给静态类型变量。

// model is of type dynamic and was passed in from the controller
@Html.TextBox("Surname", model.Surname) <-- this breaks

@{ string sn = model.Surname; }
@Html.TextBox("Surname", sn); <-- will work

如果您创建自定义配置文件类,如上所述Joel,ASP.Net仍将生成ProfileCommon类,但它将继承自您的自定义配置文件类。如果您未指定自定义配置文件类,则ProfileCommon将从System.Web.Profile.ProfileBase继承。

如果您创建自己的配置文件类,请确保未在自定义配置文件类中已声明的Web.config文件中指定配置文件属性。如果你这样做,ASP.Net在尝试生成ProfileCommon类时会给出编译器错误。

配置文件也可以在Web应用程序项目中使用。 可以在设计时或以编程方式在Web.config中定义属性。在Web.config中:

<profile enabled="true" automaticSaveEnabled="true" defaultProvider="AspNetSqlProfileProvider">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="TestRolesNProfiles"/>
      </providers>
      <properties>
        <add name="FirstName"/>
        <add name="LastName"/>
        <add name ="Street"/>
        <add name="Address2"/>
        <add name="City"/>
        <add name="ZIP"/>
        <add name="HomePhone"/>
        <add name="MobilePhone"/>
        <add name="DOB"/>

      </properties>
    </profile>

或以编程方式,通过实例化 ProfileSection 并使用 ProfilePropertySettings ProfilePropertySettingsColletion 创建单个属性来创建配置文件部分,所有这些都在系统中.Web.Configuration命名空间。 要使用配置文件的这些属性,请使用System.Web.Profile.ProfileBase对象。如上所述,无法使用配置文件。语法访问配置文件属性,但可以通过实例化ProfileBase并使用 SetPropertyValue (<!> quot; PropertyName)轻松完成 <!>“;)和 GetPropertyValue {<!>”; PropertyName <!>“;)如下:

ProfileBase curProfile = ProfileBase.Create("MyName");

或访问当前用户的个人资料:

ProfileBase curProfile = ProfileBase.Create(System.Web.Security.Membership.GetUser().UserName);



        curProfile.SetPropertyValue("FirstName", this.txtName.Text);
        curProfile.SetPropertyValue("LastName", this.txtLname.Text);
        curProfile.SetPropertyValue("Street", this.txtStreet.Text);
        curProfile.SetPropertyValue("Address2", this.txtAdd2.Text);
        curProfile.SetPropertyValue("ZIP", this.txtZip.Text);
        curProfile.SetPropertyValue("MobilePhone", txtMphone.Text);
        curProfile.SetPropertyValue("HomePhone", txtHphone.Text);
        curProfile.SetPropertyValue("DOB", txtDob.Text);
        curProfile.Save();

当您在 Visual Studio 中创建新的网站项目时,将从配置文件返回的对象将(自动)为您生成。当您创建 Web 应用程序项目或 MVC 项目时,您必须推出自己的项目。

这听起来可能比实际上更困难。您需要执行以下操作:

  • 使用创建数据库 aspnet_regsql.exe 该工具与 .NET 框架一起安装。
  • 编写一个从 ProfileGroupBase 派生的类,或者安装 Web Profile Builder (WPB),它可以根据 Web.Config 中的定义为您生成该类。我已经使用 WPB 一段时间了,到目前为止它已经达到了预期的效果。如果您有很多属性,使用 WPB 可以节省大量时间。
  • 确保在 Web.Config 中正确配置了与数据库的连接。
  • 现在您可以创建配置文件类的实例(在控制器中)
  • 您可能需要在视图中使用配置文件属性值。我喜欢将配置文件对象本身传递给视图(而不是单个属性)。

如果您使用的是Web应用程序项目,则无法在设计时即时访问Profile对象。这是一个可以为你做的实用工具: http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx 。就个人而言,该实用程序在我的项目中导致错误,所以我最终滚动自己的配置文件类继承自ProfileBase。这根本不难做到。

用于创建自定义类的MSDN演练(a.k.a. Joel的方法):
http://msdn.microsoft.com/en-us/magazine/cc163624。 ASPX

我也遇到了同样的问题。但是我没有创建一个继承自ProfileBase的类,而是使用了HttpContext。

在web.config文件中指定属性,如下所示: -

现在,编写以下代码: -

编译并运行代码。您将获得以下输出: -

网页配置文件生成器 对我来说效果很好。它生成的类比 Joel 的帖子中描述的要多得多。我不知道它是否真的需要或有用。

无论如何,对于那些寻找一种简单方法来生成类,但又不想有外部构建工具依赖项的人来说,您始终可以

  • 使用网络配置文件生成器
  • 删除它的所有痕迹!
  • 继续使用生成的 Profile 类

或者(未经测试但可能有效)

  • 创建一个网络 地点 项目
  • 创建你的元素
  • 捕捉生成的类并将其复制到您的网络 项目 项目

如果第二种方法确实有效,有人可以告诉我以供将来参考吗

只想添加Joel Spolsky的回答

我实施了他的解决方案,工作出色btw - Cudos!

对于想要获取不是我使用的登录用户的用户个人资料的任何人:

的web.config:

  <connectionStrings>
    <clear />
    <add name="LocalSqlConnection" connectionString="Data Source=***;Database=***;User Id=***;Password=***;Initial Catalog=***;Integrated Security=false" providerName="System.Data.SqlClient" />
  </connectionStrings>

<profile defaultProvider="SqlProvider" inherits="NameSpace.AccountProfile" enabled="true">
  <providers>
    <clear/>
    <add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="LocalSqlConnection"/>
  </providers>

然后是我的自定义类:

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

namespace NameSpace
{
    public class AccountProfile : ProfileBase
    {
        static public AccountProfile CurrentUser
        {
            get
            {
                return (AccountProfile)
                 (ProfileBase.Create(Membership.GetUser().UserName));
            }
        }

        static public AccountProfile GetUser(MembershipUser User)
        {
            return (AccountProfile)
                (ProfileBase.Create(User.UserName));
        }

        /// <summary>
        /// Find user with matching barcode, if no user is found function throws exception
        /// </summary>
        /// <param name="Barcode">The barcode to compare against the user barcode</param>
        /// <returns>The AccountProfile class with matching barcode or null if the user is not found</returns>
        static public AccountProfile GetUser(string Barcode)
        {
            MembershipUserCollection muc = Membership.GetAllUsers();

            foreach (MembershipUser user in muc)
            {
                if (AccountProfile.GetUser(user).Barcode == Barcode)
                {
                    return (AccountProfile)
                        (ProfileBase.Create(user.UserName));
                }
            }
            throw new Exception("User does not exist");
        }

        public bool isOnJob
        {
            get { return (bool)(base["isOnJob"]); }
            set { base["isOnJob"] = value; Save(); }
        }

        public string Barcode
        {
            get { return (string)(base["Barcode"]); }
            set { base["Barcode"] = value; Save(); }
        }
    }
}

像魅力一样......

很棒的帖子,

只是关于web.config的说明 如果你没有在profile元素中指定inherit属性 您需要在配置文件中指定每个indiviudal配置文件属性 web.config上的元素如下所示

 <properties>
    <clear/>
    <add name="property-name-1" />
    <add name="property-name-2" />
    ..........

 </properties>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top