Click here to Skip to main content
11,932,461 members (46,921 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked

Get an unique session in each browser tab

, 17 Feb 2012 LGPL3
Rate this:
Please Sign up or sign in to vote.
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 Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.

blog | twitter

You may also be interested in...

Comments and Discussions

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 
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
mvpSandeep 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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 17 Feb 2012
Article Copyright 2012 by jgauffin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid