Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a Profile in an ASP.NET Web Application [duplicate]

Tags:

asp.net

Possible Duplicate:
How to assign Profile values?

I'm reading an ASP.NET book that says you can't use Profile if you select Web Application when you start a project. It only run under Web Site.

Are there any alternatives for Web Application? Or do you need to build your own Profile system.

like image 463
TeaDrinkingGeek Avatar asked Mar 27 '11 15:03

TeaDrinkingGeek


1 Answers

You can use the profile provider if you are using a web application instead of a web site, but out of the box from an aspx page code behind you will not be able to do Profile.MyProperty.

This isn't a big deal as with a little bit of effort you will be able to do something similar. The example providing in Converting a Web Site Project to a Web Application Project is a good jumping off point on how to use the profile provider with a web application.

To summarize the Converting Profile Object Code section of the aforementioned article create a ProfileCommon class

public class ProfileCommon
{
    public Teachers Teachers
    {
        get
        {
            return (Teachers)
                HttpContext.Current.Profile.GetPropertyValue("Teachers");
        }
        set
        {
            HttpContext.Current.Profile.SetPropertyValue("Teachers",value);
        }
    }
}

Then on your aspx code behind you can now do

ProfileCommon Profile = new ProfileCommon();
protected void Button1_Click(object sender, EventArgs e)
{
    Teachers teachers = new Teachers();
    teachers.Add(new Teacher("scott"));
    teachers.Add(new Teacher("bob"));
    teachers.Add(new Teacher("paul"));

    Profile.Teachers = teachers;    
}

An alternative to instantiating the ProfileCommon as a field for each page would be to make it and its methods static and just call the class properties from the code behinds Profile.Teachers

public static class Profile
{
    public static Teachers Teachers
    {
        //......
    }
}

This isn't a huge advantage, but makes your code more similar to that of a ASP.NET Web Site project.

Editorial Commentary not pertinent to the answer
There are key differences between the two project types, but for my projects I prefer web applications. I have a bias because creating a build profile for the TFS Build Manager is much easier for Web Application projects than it is for Web Site projects. Further it seems like Microsoft emphasized Web Site projects and then backed away from them in favor of Web Application projects.

like image 179
ahsteele Avatar answered Nov 15 '22 04:11

ahsteele