2

I have the following code that creates a band profile:

    var bandProfile = _profileService.CreateBandProfile(model.BandProfile, file, UserId);

    if (bandProfile != null)
    {
        userManager.AddToRole(UserId, "Band");
        //Store the bandprofile ID anywhere?
        return RedirectToAction("Index", "Welcome");
    }

No I want to store and make the bandprofile ID accessible through the application. Keep It accessible while the user is logged in with the profile.

How can I accomplish this?

For example, to get the userId, you can do like this through the application:

UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

I want to do the same thing, but with bandprofileId.

Bryan
  • 3,421
  • 8
  • 37
  • 77

1 Answers1

0

There is some debate as to the "correctness" of doing so (linked below), but you can store the variable in HttpContext.Current.Application["BandProfile"].

if (bandProfile != null)
{
    userManager.AddToRole(UserId, "Band");
    //Store the bandprofile ID anywhere?
    HttpContext.Current.Application["BandProfile"] = bandProfile;

    return RedirectToAction("Index", "Welcome");
}

Alternatively, you can use a static variable in a class somewhere.

public static class BandProfile
{
    public static whatever Profile;
}

if (bandProfile != null)
{
    userManager.AddToRole(UserId, "Band");
    //Store the bandprofile ID anywhere?
    BandProfile.Profile = bandProfile;

    return RedirectToAction("Index", "Welcome");
}

Here is a related question that deals with the same issue, and here is another.

EDIT:

To then access these variables, you can use

var bandProfile = HttpContext.Current.Application["BandProfile"];

or

var bandProfile = BandProfile.Profile;

According to Microsoft:

ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP.NET. It is recommended that you store data in static members of the application class instead of in the Application object.

That said, you should use the static variable method. Static variables are available by calling ClassName.Variable and will exist for the duration of the app running. If the app is closed or the variable is otherwise changed, you will lose this information.

In order to save the information, it's necessary to write the contents of this variable to an external source (DB, file, etc.) and read it in when the app starts.

Community
  • 1
  • 1
levelonehuman
  • 1,465
  • 14
  • 23
  • When I assign the class-variable the value, Is the value then stored "for ever" in that variable? – Bryan Apr 08 '16 at 13:27
  • @Bryan I'm trying to find a more concrete answer for you, but my understanding is that the variable is set for the duration of the user's session. It won't become a global variable that's always there "forever". – levelonehuman Apr 08 '16 at 13:30
  • Although, you may need to use `HttpContext.Current.Session` for that. Let me see what I can find – levelonehuman Apr 08 '16 at 13:30
  • Also - if you're using the `static` class, this will be for the duration of the app running and will be reset unless the value is saved (to DB, for instance) and read in later. I don't know enough to tell you how this will work with multiple users accessing the content simultaneously, though. – levelonehuman Apr 08 '16 at 13:31
  • @Bryan I have updated the answer to include some info from Microsoft (and my own conclusions) on the issue. – levelonehuman Apr 08 '16 at 13:43