在ASP.NET MVC多个浏览器标签唯一的会话 [英] Unique session in multiple browser tabs in ASP.NET MVC

查看:179
本文介绍了在ASP.NET MVC多个浏览器标签唯一的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建每当用户打开ASP.NET MVC应用程序中的新的浏览器标签或窗口唯一的会话。是否有可能在ASP.NET / MVC?

I want to create unique session whenever user open a new browser tab or window in ASP.NET MVC application. Is it possible in ASP.NET / MVC ?

我试着按照下面的解决方案

I tried to follow below solution

<一个href=\"http://stackoverflow.com/questions/2840615/asp-net-session-multiple-browser-tabs-different-sessions\">asp.net - 会话 - 多个浏览器标签 - 不同的会话

然而,它没有在ASP.NET MVC工作。上面的解决方案使用ViewState的,在MVC我试着使用TempData的,但它不能正常工作。

However it doesn't work in ASP.NET MVC. Above solution using ViewState, in MVC I tried using TempData but it doesn't work.

推荐答案

如你所知,HTTP是无状态的一个非常普遍的机制,有请求之间的状态是使用会话变量。当你打开一个新的标签布劳尔因为会话是相同的,所以你在新的选项卡中进行任何更改是要去影响其他选项卡时出现问题。
你没有明确指定你想要做的,但让我们假设你有一个产品列表页面,用户可以输入搜索过滤器,你想将它们保存在会话的内容。如果用户在设置选项卡1搜索过滤器值,标签2是要去有相同的值(它们共享会话变量)。你能做什么?

As you know, HTTP is stateless and a very common mechanism to have state between requests is use session variables. The problem occurs when you open a new brower tab because the session is the same so any change you make in the new tab is gonna impact other tabs. You didn't specify exactly what you want to do but let's say that you have a product list page where the user can enter search filters and you want to save them in the session. If the user sets a search filter value in tab 1, tab 2 is gonna have the same value (they share session variables). What you can do?

1),使用此方法添加URL中的GUID:http://www.$c$cproject.com/Articles/331609/Get-an-unique-session-in-each-browser-tab

1) Use this approach for adding a guid in the URL: http://www.codeproject.com/Articles/331609/Get-an-unique-session-in-each-browser-tab

2)做类似什么在previous点,但不能以同样的方式描述的,这是我做过什么来解决同样的问题什么的。

2) Do something similar to what's described in the previous point but not in the same way and this is what I did to solve the same problem.

一)我的检索算法页面链接/产品/列表?GUID = XXX,而不是仅仅/产品/列表。如果用户手动类型/产品/列表,我重定向到他所在的GUID设置一个新的URL。

a) My links to the serach page are /Product/List?guid=xxx instead of just /Product/List. If the user manually types /Product/List, I'm redirecting him to a new URL where the GUID is set.

public ActionResult List(string guid)
        {
            if (guid == null)
            {
                return RedirectToAction("List", new { guid = Guid.NewGuid().ToString() });
            }
...

你的名单点击链接,目标一个新的标签每当一个新的GUID生成的。

Every time you click on the "list" link and target a new tab, a new GUID is generated.

二)我与GUID的会话密钥,以便每一页都有自己的价值观。你可以有2个选项卡在同一时间打开,他们会使用不同的会话值,因为GUID是会有所不同。

b) I have a session key with the GUID so each page has its own values. You can have 2 tabs opened at the same time and they are gonna be using different session values because the guid is gonna be different.

这方案并不完美,但至少它的作品。

This solution is not perfect but at least it works.

我希望它能帮助。

这篇关于在ASP.NET MVC多个浏览器标签唯一的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆