ASP.Net会话 [英] ASP.Net Session
问题描述
我想储存一些动作的用户在一系列不同的ASP.Net Web表单的正在执行的国家。什么是我的坚持为国家的选择,有什么利弊/每个解决方案的利弊?
I am wanting to store the "state" of some actions the user is performing in a series of different ASP.Net webforms. What are my choices for persisting state, and what are the pros/cons of each solution?
我一直在使用Session对象,并使用一些辅助方法来强类型的对象:
I have been using Session objects, and using some helper methods to strongly type the objects:
public static Account GetCurrentAccount(HttpSessionState session)
{
return (Account)session[ACCOUNT];
}
public static void SetCurrentAccount(Account obj, HttpSessionState session)
{
session[ACCOUNT] = obj;
}
有人告诉我无数源的会话是邪恶的,所以这确实是这个问题的根本原因。我想知道你在想什么最佳实践,以及为什么。
I have been told by numerous sources that "Session is evil", so that is really the root cause of this question. I want to know what you think "best practice", and why.
推荐答案
有什么本质邪的会话状态。
There is nothing inherently evil with session state.
有几件事情要记住,可能会咬你,但:
There are a couple of things to keep in mind that might bite you though:
- 如果用户presses浏览器的后退按钮,返回到previous页面,但您的会话状态不恢复。所以你CurrentAccount可能不是什么它原本是在页面上。
- ASP.NET进程可以通过IIS得到回收。当发生这种情况,你下一个请求将启动一个新的进程。如果您在使用过程中的会话状态,在默认情况下,它会消失: - (
- 会话也可以用同样的结果超时,如果用户不是一段时间的活性。默认为20分钟,一个不错的午餐会做到这一点。
- 进程外会话状态的使用需要出存储在会话状态的所有对象是可序列化。
- 如果用户打开一个浏览器窗口,他会期望有一个第二和独特的应用程序,但会话状态最有可能会被两个之间共享。因此,改变一个浏览器窗口中的CurrentAccount将做同样在其他。
这篇关于ASP.Net会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!