.Net 会话范围变量?

发布于 2024-10-13 17:45:09 字数 157 浏览 2 评论 0原文

我是 .net 的新手,我正在使用 MVC2 + linQ 和 C#

我想知道是否可以使用会话作用域变量或类以及如何做到这一点? 我需要将其存储在会话对象中吗?或者只是声明一个全局变量?

我需要这个来存储来自不同sql表的数据关联到登录用户

提前致谢

I'm new in .net and i'm using
MVC2 + linQ and C#

I want to know if is possible to use session scoped variables or clases and how to do that?
Do i Need to store it in a session object ? or just to declare a global variable ?

I need this to store data from diferent sql tables associates to the logged in user

Thanks in advance

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

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

发布评论

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

评论(5

飘过的浮云 2024-10-20 17:45:09

是的,您应该能够使用会话 控制器操作方法中的对象来存储和检索会话数据。

Session["MyData"] = myData;
MyDataType myData = (MyDataType)Session["MyData"];

显然,要小心您存储的对象的大小以及会话数据的处理方式,即在进程、SQL Server 或应用程序服务器中。如果您没有将会话数据存储在内存中,那么您将需要确保它是可序列化的。

Yes, you should be able to use the Session object from within your Controller's action methods to store and retrieve session data.

Session["MyData"] = myData;
MyDataType myData = (MyDataType)Session["MyData"];

Obviously be careful with the size of objects you are storing and how Session data is handled i.e. in process, SQL Server or application server. If you are not storing your session data in memory then you will need to ensure that it is serializable.

念﹏祤嫣 2024-10-20 17:45:09

是的,因为它是会话范围的数据,所以会话将是放置该数据的地方,这没有任何问题。

我能看到的唯一缺点是,如果会话超时,您会丢失该数据 - 因此,如果您想要保留该数据(而不是只读或临时数据),您可能希望将状态保存在您的数据库代替。

Yes since it's session scoped data, the session would be to the place to put that data, there's nothing wrong with that.

The only down side I can see is that if the session times out you lose that data - so if it's data that you want to keep (instead of i.e. just read-only or temporary data), you probably want to save the state in your DB instead.

通知家属抬走 2024-10-20 17:45:09

控制器会为每个请求重新创建,因此如果您想在每个会话中存储某些内容,是的,您需要将其存储在 Session["key"] 中。

另一个可能有意义的选项(取决于您正在做什么)是使用 DI 容器通过构造函数注入会话作用域变量,并使用 DI 工具将其作用域配置为每个会话。

Controllers are re-created for every request, so if you want to store something per session, Yes, you need to store it in the Session["key"].

Another option that might make sense (depending upon what you're doing) would be to use a DI Container to inject the session scoped variables via the constructor, and configure their scope with the DI tool to be per session.

撩起发的微风 2024-10-20 17:45:09

正如其他人所说,语法很简单,如果需要,您可以简单地将 Session 视为哈希图或字典。

Session["userData"] = userData;
UserInfo userData = Session["userData"] as UserInfo;

请注意(因为您是 C# 新手),“as”关键字基本上是可为 null 的强制转换;如果转换失败,您的 userData 变量将被设置为 null。

为了额外的好处,(并且根据您的应用程序的要求)您可以将所有数据访问包装在 DAO 或类似存储库的对象中,并让该对象在幕后使用 Session。数据存储的位置是控制器本身不需要的实现细节;从数据库(或 xml 文件、静态内存对象等)的会话中获取它应该与控制器无关(其主要工作应该是控制应用程序流)。

As others have said the syntax is straightforward, you can simply treat Session like a hashmap or Dictionary if you want.

Session["userData"] = userData;
UserInfo userData = Session["userData"] as UserInfo;

Note (since you're new to C#) that the "as" keyword is basically a nullable cast; if the cast fails your userData variable will be set to null.

For extra credit, (and depending on your app's requirements) you could wrap all data access in a DAO or repository-like object, and have that object use Session under the hood. The location of data storage is an implementation detail that the controller itself doesn't need; fetching it from the Session vice the database (or an xml file, or static in-memory objects, or whatever) should be irrelevant to the controller (whose main job should be controlling application flow).

悟红尘 2024-10-20 17:45:09

简而言之,您不应该使用全局变量 - 它们仅在单个请求期间起作用。因此,您必须使用 Session 属性。

但无论如何你应该避免使用它。

并且您始终可以使用

编辑:我不是来自英语国家,所以有时我应该使用“should not”时使用“cannot”。为了避免更多的反对,让我们在这里澄清一些事情 - 当然,您可以在网络多线程应用程序中使用全局变量。这不是很聪明,因为所有引用的对象都将在请求结束时丢失。但请随意这样做。

The short answer is that you should not use global variables - they only work during a single request. So you would have to use the Session property.

But you should avoid using it anyway IMHO.

And you always may use the UserData member of the forms ticket if you want to have custom data associated to the logged in user.

Edit: I am not from an English speaking country, so sometimes I use "cannot" when I should be using "should not". To avoid any more downvoting, let's make something clear here - of course you can use global variables in a web, multithreaded application. That's just not very smart, since all referenced objects will be lost at the end of the request. But feel free to do it.

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