How to assign Profile values?

前端 未结 10 647
别那么骄傲
别那么骄傲 2020-11-22 08:49

I don\'t know what I am missing, but I added Profile properties in the Web.config file but cannot access Profile.Item in the code or create a new profile.

相关标签:
10条回答
  • 2020-11-22 09:17

    When you create a new Web site project in Visual Studio then the object that is returned from Profile will be (automatically) generated for you. When you create a Web application project or an MVC project, you will have to roll your own.

    This probably sounds more difficult than it is. You need to do the following:

    • Create a database using aspnet_regsql.exe This tool is installed along with the .NET framework.
    • Write a class that derives from ProfileGroupBase or install the Web Profile Builder (WPB) that can generate the class for you from the definition in Web.Config. I have been using WPB for a while and up until now it has done what is expected of it. If you have a lot of properties, using WPB can save quite a bit of time.
    • Make sure the connection to the database is properly configured in Web.Config.
    • Now you are set to create an instance of your profile class (in the controller)
    • You will probably need the profile property values in your views. I like to pass the profile object itself along to the view (not individual properties).
    0 讨论(0)
  • 2020-11-22 09:17

    The Web Profile Builder worked great for me. The class it generated has a lot more in it than as described by Joel's post. Whether or not its actually needed or useful I dont know.

    Anyway for those looking for an easy way to generate the class, but not wanting to have an external build tool dependency you can always

    • use the web profile builder
    • delete all trace of it!
    • keep using the generated Profile class

    OR (untested but may just work)

    • create a web site project
    • create your element
    • snap the generated class and copy it over to your web project project

    if this second approach does work can someone let me know for future reference

    0 讨论(0)
  • 2020-11-22 09:19

    If you are using a web application project, you cannot access the Profile object at design-time out-of-the-box. Here is a utility that supposedly does it for you: http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx. Personally, that utility caused an error in my project so I ended up rolling my own profile class to inherit from ProfileBase. It was not hard to do at all.

    0 讨论(0)
  • 2020-11-22 09:19

    I was also running through the same issue. But instead of creating a class which inherits from ProfileBase, I used the HttpContext.

    Specify properties in web.config file as follows : - ProfilePropertyWeb.config

    Now, write the following code : -

    Code Behind Profile Properties

    Compile and run the code. You will get following output: -

    Output

    0 讨论(0)
提交回复
热议问题