【问题标题】:How can I access web.config Profile properties in ASP.NET 4.5?如何访问 ASP.NET 4.5 中的 web.config 配置文件属性?
【发布时间】: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() 问题。解决后,您可能会发现您的个人资料问题已相关和/或已解决。

标签: c# asp.net .net


【解决方案1】:

根据您的描述,我认为问题在于它引用不正确,因此无法找到您要访问的内容。

我猜你试过了: How to assign Profile values?

正如您提到的作者,Joel Spolskys 回答的第一部分是您使用的是哪种应用程序类型??? Visual Studio 中有两种项目——“网站项目”和“Web 应用程序项目”。 还有其他几种类型的项目,但这些是主要的“网络”应用程序。

如何访问属性的具体细节因框架和应用程序类型而异。例如,如果您在 Windows 表单中拥有 app.config,或者如果您在 Web 应用程序中拥有 web.config。您是否制作了 web.config 或者它在那里并且您已经对其进行了编辑?

我可以尝试解释如何访问这些属性,但如果它与您的应用程序的类型不同,它无论如何都不会工作。

您可以先检查一下吗 determine project type in visual studio?

如果您使用了错误的项目类型,只需尝试以正确的类型创建一个新项目,然后查看您现有的代码是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多