【发布时间】:2014-01-28 15:23:27
【问题描述】:
我浏览了 ASP.NET 4.0 在线课程,这只是最初的课程之一。
我已经安装了 Visual Studio 2012 for Web,它搭载了 ASP.NET 4.5。
为了访问一些 web.config 属性,课程中的人写了以下行:
Profile.Preferences.MyParameter = "MyValue";
但是当我输入相同的内容时,它给了我一个错误:Profile element doesn't exist in the current context。当我尝试访问System.Web.Profile.Preferences 时,它会显示Type or namespace "Preferences" is missing in System.Web.Profile。
谁能告诉我发生了什么? ASP.NET 4.5 是这种行为的原因吗?如果是 - 我如何访问与 ASP.NET 4.5 相同的 web.config 属性?
我的 web.config 看起来像这样:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<profile>
<properties>
<group name="Preferences">
<add name="MyParameter"/>
</group>
</properties>
</profile>
</system.web>
</configuration>
更新: 我试图从 Joel Spolsky 的回答中实现第二个解决方案:How to assign Profile values?
但是代码对我不起作用。以下方法崩溃:
static public AccountProfile CurrentUser
{
get { return (AccountProfile)
(ProfileBase.Create(Membership.GetUser().UserName)); }
}
这是因为 Membership.GetUser() 返回 null。我能用它做什么?
【问题讨论】:
-
你想做什么?您似乎没有尝试访问 web.config 属性。您可以使用 ProfileSection 访问配置的配置文件部分。
-
@MikeSmithDev 我已将 web.config 内容添加到问题中。我需要为
"MyParameter"赋值。 -
您可以先尝试解决您的 Membership.GetUser() 问题。解决后,您可能会发现您的个人资料问题已相关和/或已解决。