简体   繁体   中英

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.

I have Visual Studio 2012 for Web installed, and it has ASP.NET 4.5 on board.

In order to access some web.config properties, guy in the course writes following line:

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 . And when I'm trying to access System.Web.Profile.Preferences , it says that 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?

My web.config looks like that:

<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?

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. 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??? There are two kinds of project in Visual Studio -- "Web Site Projects" and "Web Application Projects." 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. Did you make the web.config or was it there and you have edited it?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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