0

Is it possible in asp.net to change the Session object so that when it is used to set/get , it does something else?

EX:

Session["variable"] I'll like it to do behind the covers Session[guid + variable] append to the key a GUID

Update:
Some of you asked way do I what to do this?
I want to do this to make the session unique for each tab in the browser. I'm forced to use the session. The guid will be a unique tab id. (see solution here)

1
  • 1
    Why would you want to do that? How would you know what key to retrieve? Commented Mar 8, 2012 at 19:36

4 Answers 4

4

You could wrap Session with your own class that pre-pends a guid to the session key, and then use that class instead of Session. With that said, I'm not sure why you would want to.

Update

If you need the data to be unique for each browser tab, use ViewState instead. It's used to persist state between requests for a particular page/browser window/tab.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 I agree. Don't see the reason to do this. It seems that OP thinks that Session and Cache are the same, hence the need for an extra unique identifier.
@jrummell yes, i know about ViewState, but the legacy code that I'm changing forces me to use Session. Thanks anyway.
@jrummell No I can not. If I could have i wouldn't have asked this question, in the first place. Thanks anyway.
Ok, but you should know that you're hammering a nail with a screw driver.
1

You can create your own session provider for ASP.NET and after this you gain full control over your session data.

Comments

1

For Webforms, you can create a base System.Web.UI.Page class that all your pages inherit from that expose your methods. Like so:

public partial class _Default : BasePage {
    protected void Page_Load(object sender, EventArgs e) {
        Person p = new Person {
            FirstName = "MyFName",
            LastName = "MyLName"
        };
        SetSessionData<Person>("somevalue", p);
        var person = GetSessionData<Person>("somevalue");
    }
}

public class BasePage : System.Web.UI.Page {
    internal void SetSessionData<T>(string name, T value) {
        this.Session[string.Format("{0}_{1}", value.GetType().GUID, name)] = value;
    }
    internal T GetSessionData<T>(string name) {
        return (T)this.Session[string.Format("{0}_{1}", typeof(T).GUID, name)];
    }
}

public class Person {
    public Person() {
        ID = Guid.NewGuid();
    }
    public Guid ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

For MVC: Another suggestion would be to create a "Base" Page class that all your controllers inherit from that expose your methods. Like so:

public class BaseController : Controller   {
    internal void SetSessionData<T>(string name, T value) {            
        this.Session[string.Format("{0}_{1}",value.GetType().GUID,name)] = value;
    }
    internal T GetSessionData<T>(string name) {
        return (T)this.Session[string.Format("{0}_{1}", typeof(T).GUID, name)];
    }
}

Then, you can get or set items as so:

        Person p = new Person{
            FirstName = "MyFName",
            LastName  = "MyLName"
        };
        SetSessionData<Person>("somevalue", p);
        var person = GetSessionData<Person>("somevalue");

2 Comments

Thanks for the suggestion, but this requires a lot of code change and testing, and i cannot afford it.
Updated code example to include non MVC application - which is basically the same.
0

It is not possible in the direct sense, without creating your own session provider. A possible work around could be to create two new methods. Possibly put them in the Global.asax

public String GetSession(String variable)
{
    return Session[guid+variable];
}

public void SetSession
{
        Session.Add(guid+variable, value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.