【发布时间】:2010-09-30 09:48:34
【问题描述】:
我不知道我缺少什么,但我在 Web.config 文件中添加了配置文件属性,但无法访问代码中的配置文件。项目或创建新配置文件。
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-membership profile
我不知道我缺少什么,但我在 Web.config 文件中添加了配置文件属性,但无法访问代码中的配置文件。项目或创建新配置文件。
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-membership profile
只想添加到 Joel Spolsky 的答案中
我实施了他的解决方案,顺便说一句,工作出色 - 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(); }
}
}
}
像魅力一样工作......
【讨论】:
Profile 也可以在 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 创建单个属性来创建配置文件部分,所有这些都在 System .Web.Configuration 命名空间。 要使用配置文件的这些属性,请使用 System.Web.Profile.ProfileBase 对象。配置文件属性无法使用上述 profile. 语法访问,但可以通过实例化 ProfileBase 并使用 SetPropertyValue("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();
【讨论】:
ProfileBase 对象并调用GetPropertyValue("PropName")?
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 类时会给出编译器错误。
【讨论】:
我也遇到了同样的问题。但是我没有创建一个继承自 ProfileBase 的类,而是使用了 HttpContext。
在 web.config 文件中指定属性如下:-
现在,编写以下代码:-
编译并运行代码。您将获得以下输出:-
【讨论】:
我今天也遇到了同样的问题,学到了很多东西。
Visual Studio 中有两种项目——“网站项目”和“Web 应用程序项目”。出于对我来说完全神秘的原因,Web 应用程序项目不能直接使用 Profile. ...强类型类不是从 Web.config 文件为您神奇地生成的,因此您必须自己动手。
MSDN 中的示例代码假定您使用的是网站项目,它们告诉您只需将 <profile> 部分添加到您的 Web.config 并与 Profile. 一起参加派对属性,但这在 Web 应用程序项目中不起作用。
您有两种选择自己滚动:
(1) 使用Web Profile Builder。这是您添加到 Visual Studio 的自定义工具,它会根据您在 Web.config 中的定义自动生成您需要的 Profile 对象。
我选择不这样做,因为我不希望我的代码依赖于这个额外的工具来编译,这可能会给其他人在他们试图构建我的代码而没有意识到他们需要的情况下造成问题这个工具。
(2) 创建您自己的派生自ProfileBase 的类来表示您的自定义配置文件。这比看起来容易。这是一个非常简单的示例,它添加了“FullName”字符串配置文件字段:
在你的 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;
【讨论】:
<profile ..><properties><add name=".."> 下的 Web 配置以及 AccountProfile 类中定义了配置文件属性,您将得到“此属性已被定义”,通过删除属性可以轻松修复在 web.config 中。
很棒的帖子,
只是关于 web.config 的注释 如果您没有在配置文件元素中指定继承属性 您将需要在配置文件中指定每个个人配置文件属性 web.config 上的元素如下
<properties>
<clear/>
<add name="property-name-1" />
<add name="property-name-2" />
..........
</properties>
【讨论】:
用于创建自定义类(又名 Joel 的方法)的 MSDN 演练:
http://msdn.microsoft.com/en-us/magazine/cc163624.aspx
【讨论】:
Web Profile Builder 非常适合我。它生成的类比 Joel 的帖子中描述的要多得多。我不知道它是否真的需要或有用。
无论如何,对于那些正在寻找一种简单的方法来生成类,但又不想拥有外部构建工具依赖项的人来说,你总是可以
或(未经测试,但可能正常)
如果第二种方法确实有效,有人可以告诉我以供将来参考
【讨论】:
当您在 Visual Studio 中创建新的网站项目时,将从 Profile 返回的对象将(自动)为您生成。当您创建 Web 应用程序项目或 MVC 项目时,您将不得不自己动手。
这听起来可能比实际更难。您需要执行以下操作:
【讨论】:
如果您使用的是 Web 应用程序项目,则无法在设计时开箱即用地访问 Profile 对象。这是一个据称可以为您完成的实用程序:http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx。就个人而言,该实用程序在我的项目中导致了错误,因此我最终滚动了自己的配置文件类以从 ProfileBase 继承。一点也不难。
【讨论】: