Click here to Skip to main content
15,123,408 members
Articles / Programming Languages / C#
Technical Blog
Posted 17 Feb 2012

Tagged as


14 bookmarked

Get an unique session in each browser tab

Rate me:
Please Sign up or sign in to vote.
4.70/5 (3 votes)
17 Feb 2012LGPL3
Get an unique session in each browser tab

I got a lot to do at work, hence the silence. I got a lot to blog about, but so little time ;/ I found an interesting question at Stackoverflow which this answer is for (also to show you that the blog is not forgotten).

A user wanted to store browser tab specific data which can be a problem since most browsers share a session between different tabs. The solution is to create a new route which generates a GUID and then use that guid to fetch and store session information. Keep in mind that you need two routes: one that works for users which just surfed into the site, and one that loads an existing guid.

Here is the route class:

public class GuidRoute : Route
    private readonly bool _isGuidRoute;

    public GuidRoute(string uri, object defaults)
        : base(uri, new RouteValueDictionary(defaults), new MvcRouteHandler())
        _isGuidRoute = uri.Contains("guid");

        DataTokens = new RouteValueDictionary();

    public override RouteData GetRouteData(HttpContextBase httpContext)
        var routeData = base.GetRouteData(httpContext);
        if (routeData == null)
            return null;

        if (!routeData.Values.ContainsKey("guid") || 
        routeData.Values["guid"].ToString() == "")
            routeData.Values["guid"] = Guid.NewGuid().ToString("N");

        return routeData;

    public override VirtualPathData GetVirtualPath
    (RequestContext requestContext, RouteValueDictionary values)
        return !_isGuidRoute ? null : base.GetVirtualPath(requestContext, values);

Replace the default route in global.asax with it:

    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional } // Parameter defaults

routes.Add("Default", new GuidRoute(
    new { controller = "Home", action = "Index", 
        guid = "", id = UrlParameter.Optional }));

routes.Add("GuidRoute", new GuidRoute(
    new { controller = "Home", action = "Index", 
        guid = "", id = UrlParameter.Optional }));

And finally some extension methods to make life easier in the controllers:

public static class ControllerExtensionMethods
    public static string GetGuid(this Controller controller)
        return controller.RouteData.Values["guid"].ToString();

    public static void SetGuidSession
    (this Controller controller, string name, object value)
        controller.Session[controller.GetGuid() + "_" + name] = value;

    public static object GetGuidSession(this Controller controller, string name)
        return controller.Session[controller.GetGuid() + "_" + name];

One thing left though: Any browser bookmarks will use an old GUID. Should not be a problem in most cases (unless the user opens the bookmark in multiple tabs = sharing the session between the tabs). Same thing goes for opening a link in a new tab = shared.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Founder 1TCompany AB
Sweden Sweden

Comments and Discussions

QuestionHow to set route for images Pin
reddiamond_pilani12-May-17 22:02
Memberreddiamond_pilani12-May-17 22:02 
QuestionControllerExtensionMethods Pin
Member 123667453-Mar-16 0:56
MemberMember 123667453-Mar-16 0:56 
QuestionRe: ControllerExtensionMethods Pin
Member 102330995-Jan-17 5:52
MemberMember 102330995-Jan-17 5:52 
GeneralAdd code sample project if you have Pin
smilanshah25-Jan-16 3:28
Membersmilanshah25-Jan-16 3:28 
QuestionNamespace Pin
MicheleLaPietra30-Mar-15 6:27
MemberMicheleLaPietra30-Mar-15 6:27 
Generalguid appending URL Pin
asp.anjan21-Jun-13 17:29
Memberasp.anjan21-Jun-13 17:29 
GeneralRe: guid appending URL Pin
jgauffin9-Sep-13 3:55
Memberjgauffin9-Sep-13 3:55 
GeneralRe: guid appending URL Pin
Member 1098130511-Sep-19 7:52
MemberMember 1098130511-Sep-19 7:52 
QuestionOpen new tab for new session? Pin
cmr_nz19-Aug-12 18:04
Membercmr_nz19-Aug-12 18:04 
GeneralMy vote of 5 Pin
Sandeep Mewara18-Jul-12 23:57
mvaSandeep Mewara18-Jul-12 23:57 
GeneralMy vote of 3 Pin
Dean Oliver22-Feb-12 9:41
MemberDean Oliver22-Feb-12 9:41 
GeneralRe: My vote of 3 Pin
jgauffin23-Feb-12 1:32
Memberjgauffin23-Feb-12 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.