每个浏览器的全局 ASP.NET 变量

发布于 2024-11-11 15:12:21 字数 291 浏览 7 评论 0原文

环境是使用C#的ASP.NET 4.0。

我打算将我需要的 ID 放入会话变量中。例如:

Session["ProjectID"] = ProjectsComboBox.SelectedValue.ToString();

但是,用户可能可以在同一台计算机上的两个不同浏览器窗口中打开我的 Web 应用程序,并处理两个不同的项目。

会话变量是每个浏览器、每个 PC 还是每次登录?

为当前浏览器窗口存储此 ID 的最佳方式是什么?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-11-18 15:12:21

我假设您正在使用默认的、基于 cookie 的会话状态管理方法。根据用户打开多个窗口的方式,每个窗口很可能被视为同一会话的一部分,因为 cookie 将在窗口之间共享。

这是一篇优秀的文章,为相同的问题提供了解决方案:

http://graciesdad.wordpress.com/2007/05/26/multiple-browser-windows-with-one-session-state/

祝你好运!

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!

银河中√捞星星 2024-11-18 15:12:21

会话变量是每个浏览器、每个 PC 还是每次登录?

默认基于 cookie 的是每个浏览器,因此通过打开选项卡或浏览器的另一个实例,会话变量将是相同的。

如果他们打开第一个浏览器之外的另一个浏览器(第一个项目他们打开 Firefox,第二个项目打开 IE),则两个浏览器的 Session 变量将不同。

为当前浏览器窗口存储此 ID 的最佳方式是什么?

如果目标是让您的用户在同一浏览器(使用选项卡或其他实例)中同时处理多个项目,我不建议使用 Session 变量。

我假设您必须保留多个页面的 ProjectID,我建议将其作为路由放入您的 URL 中,并将数据返回到您的页面上。

您的 URL 可能类似于 /Project/Edit/12345 。

以下是 Global.asax.cs 文件的示例

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Project/{action}/{projectId}",
        "~/Project.aspx");
}

在 Project.aspx.cs 上,您可以像这样访问路由数据:

int year = Convert.ToInt32(Page.RouteData.Values["projectID"])

有关路由的更多信息,您可以参考此内容:

http://msdn.microsoft.com/en-us/library/cc668201.aspx#routes

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文