asp.net-mvc 仍然可以在 ASP.NET mvc 中使用 Session 变量,或者对于某些东西(例如购物车)是否有更好的选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6744493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Still ok to use Session variables in ASP.NET mvc, or is there a better alternative for some things (like a cart)
提问by Keeno
I have a situation where I need access to a shopping cart over several pages. So, on the product page - create the cart an add some items On the cart checkout page - confirm the billing address On the cart checkout post - do a final check, add cart to DB and go off to payment
我有一种情况,我需要访问多个页面的购物车。因此,在产品页面 - 创建购物车并添加一些项目 在购物车结帐页面 - 确认帐单地址 在购物车结帐后 - 进行最后检查,将购物车添加到数据库并开始付款
My question is, whats the best way to pass around the cart?
我的问题是,绕过购物车的最佳方式是什么?
I have tried passing the Cart from page to postback and keeping all the values alive, however on some pages (the billing address confirmation page) this seems like a lot of hassle, all I want to check is the billing address and dont really want tons of HiddenFor() on the page to populate the cart back again
我尝试将购物车从页面传递到回发并保持所有值有效,但是在某些页面(账单地址确认页面)上这似乎很麻烦,我只想检查账单地址而不是真的想要吨HiddenFor() 在页面上再次填充购物车
TempData[] is what I used for the product to checkout page, then wondered is it best to keep on setting TempData all the time when....
TempData[] 是我用于产品结帐页面的内容,然后想知道是否最好一直设置 TempData,...
you could just use a session variable?
你可以只使用会话变量吗?
For some reason I read its not great practice to use Session, hence the question.
出于某种原因,我读到使用 Session 不是很好的做法,因此是这个问题。
Thanks for your guidance, I can happy provide some code/more info if you deem it helpful.
感谢您的指导,如果您认为有帮助,我很乐意提供一些代码/更多信息。
回答by Zruty
It is perfectly OK to use sessions in ASP.NET MVC, especially in the shopping cart scenario of yours.
在 ASP.NET MVC 中使用会话是完全可以的,尤其是在您的购物车场景中。
There are drawbacks of using sessions, but they seem not to apply to your case:
使用会话有一些缺点,但它们似乎不适用于您的情况:
1) The sessions prevent a user to properly browse your site from multiple browser tabs, the changes made in one tab are reflected in all others. But with a shopping cart, it's exactly what you need. You don't need several shopping carts per user, do you?
1) 会话阻止用户从多个浏览器选项卡正确浏览您的站点,在一个选项卡中所做的更改会反映在所有其他选项卡中。但是有了购物车,这正是您所需要的。每个用户不需要几个购物车,对吗?
2) The sessions aren't persisted by default, and if you're operating on a webfarm, you need to save the sessions in your database to be accessible by every farm node. But it seems unlikely that you're scaling like this. And if you meet the scaling neccessity, sessions won't be your top problems.
2) 默认情况下不会保留会话,如果您在网络农场上操作,则需要将会话保存在数据库中,以便每个农场节点都可以访问。但是您似乎不太可能像这样扩展。如果您满足扩展的必要性,会话将不是您的首要问题。
3) Sessions require additional functionality from the user's browser (typically, cookies). But modern browsers all support cookies, so you only have to worry about very special browsers.
3) 会话需要用户浏览器的附加功能(通常是 cookie)。但是现代浏览器都支持cookies,所以你只需要担心非常特殊的浏览器。
There are also some benefits of the sessions over hidden inputs:
会话相对于隐藏输入还有一些好处:
1) The smaller overhead. Only a small session cookie is passed back and forth between you and the client, rather than the complete set of hidden inputs.
1)较小的开销。在您和客户端之间来回传递的只是一个小的会话 cookie,而不是完整的隐藏输入集。
2) Simpler programming. You don't have to make sure you included your hidden inputs in every single one of your pages.
2) 更简单的编程。您不必确保在每个页面中都包含隐藏的输入。
3) Security. The client can alter the contents of hidden inputs however he pleases. You can't easily pass sensitive information via hidden inputs, you need to encrypt it. Session values are stored on the server, so the client doesn't have access to them.
3) 安全。客户端可以随意更改隐藏输入的内容。您无法通过隐藏输入轻松传递敏感信息,您需要对其进行加密。会话值存储在服务器上,因此客户端无法访问它们。
回答by James McCormack
Sessions are fine, but consider the Amazon-style system whereby you are issued with a recognition cookie even when you are not logged in. This allows them to store your shopping basket in the database, keyed against the recognition cookie.
会话很好,但请考虑亚马逊风格的系统,即使您未登录,系统也会向您发出识别 cookie。这允许他们将您的购物篮存储在数据库中,根据识别 cookie 键入。
The result is that you avoid the horrible user experience of losing your shopping basket due to session timeout / server appdomain recycling (the latter is mitigated by using SQLState session storage, which I recommend). The user can come back days later and their basket will still be there. Unless that's a security / privacy problem, I find it the better solution.
结果是您避免了由于会话超时/服务器应用程序域回收而丢失购物篮的可怕用户体验(后者可以通过使用 SQLState 会话存储来缓解,我建议这样做)。用户可以在几天后回来,他们的购物篮仍然在那里。除非这是一个安全/隐私问题,否则我认为这是更好的解决方案。
回答by Muhammad Adeel Zahid
It is very much ok to use session with asp.net mvc application. steve sanderson has used session for cart in sample application that comes with his book. The code is available here
将会话与 asp.net mvc 应用程序一起使用非常好。史蒂夫桑德森在他的书附带的示例应用程序中使用了购物车会话。代码可以在这里找到
回答by Palantir
I would use Session, unless there were reasons to avoid it.
我会使用 Session,除非有理由避免它。
For example, I have one project where I have repeated calls to an MVC action in the background. This action serves a file, which is slow over the network. I used to use Session, but I quickly discovered the main adverse effect: IIS won't execute calls from the same user in parallel, but only sequentially one after the other. This had a dramatic impact on performance, so I used an alternative method: I set HttpContext.User.Identity to the username, and use it as the key to fetch things from the database. But you could probably set it to some random GUID and have this to replace Sessions.
例如,我有一个项目,我在后台重复调用 MVC 操作。此操作提供一个文件,该文件在网络上速度较慢。我以前用过Session,但很快就发现了主要的不利影响:IIS不会并行执行来自同一用户的调用,而是一个接一个地依次执行。这对性能产生了巨大的影响,所以我使用了另一种方法:我将 HttpContext.User.Identity 设置为用户名,并将其用作从数据库中获取内容的键。但是你可以将它设置为一些随机的 GUID 并用它来替换 Sessions。
回答by NightOwl888
For a shopping cart, you should definitely not use session state. A sound approach is to use the Anonymous Identification Moduleto manage a cookie for you. All you need is one line in web.config.
对于购物车,你绝对不应该使用 session state。一种合理的方法是使用匿名识别模块为您管理 cookie。您只需要一行web.config。
<system.web>
<anonymousIdentification enabled="true" />
</system.web>
Then, on each request you can use the Request.AnonymousIDproperty (which returns a string representing a GUID) to lookup the shopping cart in the database.
然后,在每个请求中,您可以使用该Request.AnonymousID属性(它返回一个表示 GUID 的字符串)在数据库中查找购物车。
public ActionResult ShowCartDetails()
{
var CartId = new Guid(Request.AnonymousID);
// Lookup cart...
return View();
}
This is not only more efficient than using session state, it is also simpler.
这不仅比使用会话状态更有效,而且更简单。
References:
参考:
回答by simonlchilds
I tend to use a cookie with my shopping cart serialized into base64 stringthis seems to work quite well
我倾向于在我的购物车中使用 cookie 序列化到base64 string这似乎工作得很好
回答by Ujwal Neupane
In cart system the products that are added to cart are very important so using session is not good idea in my view. Using cookies and a temporary table in database is one of best Idea. We can store those data for forever or can clear after certain days.
在购物车系统中,添加到购物车的产品非常重要,因此在我看来使用 session 不是一个好主意。在数据库中使用 cookie 和临时表是最好的主意之一。我们可以永久存储这些数据,也可以在某些天后清除。

