Gobal ASP.NET variable per browser
Environment is ASP.NET 4.0 using C#.
I was going to put an ID that I need in a session variable. For example:
Session["ProjectID"] = ProjectsComboBox.SelectedValue.ToString();
However, it is possible that the user could have my web application open on the same computer in two different browser windows and be working开发者_开发技巧 on two different Projects.
Is the session variable per browser, per PC, or per login?
What is the best way to store this ID for the current browser window?
Thanks!
I'm assuming you're using the default, cookie-based method of session state management. Depending on how the user opens up their multiple windows, it is very likely that each window will be considered part of the same session, as the cookie will be shared between windows.
Here is an excellent article that provides a solution for the identical problem:
http://graciesdad.wordpress.com/2007/05/26/multiple-browser-windows-with-one-session-state/
Good luck!
Is the session variable per browser, per PC, or per login?
The default cookie-based is per browser, so by opening a tab or another instance of the browser the Session variable would be the same.
If they open another browser other than the first (first Project they open Firefox and IE for the second) the Session variable would be different for the two.
What is the best way to store this ID for the current browser window?
If the goal is to have your users working on multiple project at the same time inside the same browser (with tab or other instance) I would not suggest going with Session variable.
I assume you have to keep the ProjectID for multiple pages, I would suggest putting it into your URL as route and getting the data back on your pages.
Your URL could look like this /Project/Edit/12345 .
Here is an example for your Global.asax.cs file
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Project/{action}/{projectId}",
"~/Project.aspx");
}
On your Project.aspx.cs you can access route data like this:
int year = Convert.ToInt32(Page.RouteData.Values["projectID"])
You can refer to this for more info on routing:
http://msdn.microsoft.com/en-us/library/cc668201.aspx#routes
精彩评论