简体   繁体   English

如何在ASP.NET 4.5中访问web.config配置文件属性?

[英]How can I access web.config Profile properties in ASP.NET 4.5?

I look through the ASP.NET 4.0 online cource, just one of the first lessons. 我查看了ASP.NET 4.0在线源代码,这只是最初的课程之一。

I have Visual Studio 2012 for Web installed, and it has ASP.NET 4.5 on board. 我安装了Visual Studio 2012 for Web,它上面有ASP.NET 4.5。

In order to access some web.config properties, guy in the course writes following line: 为了访问一些web.config属性,课程中的人写下以下行:

Profile.Preferences.MyParameter = "MyValue";

but when I type the same thing, it gives me an error: Profile element doesn't exist in the current context . 但是当我输入相同的内容时,它会给我一个错误: Profile element doesn't exist in the current context And when I'm trying to access System.Web.Profile.Preferences , it says that Type or namespace "Preferences" is missing in System.Web.Profile . 当我尝试访问System.Web.Profile.Preferences ,它表示Type or namespace "Preferences" is missing in System.Web.Profile

Can anyone please tell what's happening? 任何人都可以告诉你发生了什么事吗? Is ASP.NET 4.5 the reason of this behavior, and if yes - how can I access the same web.config properties is ASP.NET 4.5? ASP.NET 4.5是这种行为的原因,如果是 - 我如何访问相同的web.config属性是ASP.NET 4.5?

My web.config looks like that: 我的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>

UPDATE: I've tried to implement the second solution from Joel Spolsky's answer: How to assign Profile values? 更新:我试图从Joel Spolsky的答案中实现第二个解决方案: 如何分配Profile值?

But the code doesn't work for me. 但代码对我不起作用。 The following method crahes: 以下方法是:

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

It happens because Membership.GetUser() returns null. 这是因为Membership.GetUser()返回null。 What Can I do with that? 我该怎么办?

From your description I believe the problem is that it is referencing improperly so it cannot find what you are trying to access. 根据您的描述,我认为问题在于它不正确地引用,因此无法找到您尝试访问的内容。

Im guessing you tried: How to assign Profile values? 我猜你试过: 如何分配个人资料值?

As you referenced the author the first part of Joel Spolskys answer is which application type are you using??? 在您引用作者时,Joel Spolskys的第一部分回答是您使用的应用程序类型??? There are two kinds of project in Visual Studio -- "Web Site Projects" and "Web Application Projects." Visual Studio中有两种项目 - “Web站点项目”和“Web应用程序项目”。 There are several other types of projects but these are the main "web" applications. 还有其他几种类型的项目,但这些是主要的“网络”应用程序。

The exact details of how to access properties varies heavily based on framework and application type. 根据框架和应用程序类型,有关如何访问属性的确切细节会有很大差异。 For example if your in Windows forms you have an app.config or if your in a web application you would have a web.config. 例如,如果您在Windows窗体中拥有app.config,或者如果您在Web应用程序中拥有web.config。 Did you make the web.config or was it there and you have edited it? 您是否制作了web.config或者它已经编辑了吗?

I could try to explain how to access the properties, but if its not for the same type as your application it won't work anyway. 我可以尝试解释如何访问属性,但如果它不是与您的应用程序相同的类型,它无论如何都不会工作。

Can you start by checking this by determine project type in visual studio ? 您可以通过在visual studio中确定项目类型来检查这一点吗?

If your in the wrong project type just try to make a new project in the correct type and see if your existing code works then. 如果您在错误的项目类型中尝试以正确的类型创建一个新项目,那么查看您现有的代码是否正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM