在 ASP.NET MVC 2 应用程序上将哪些数据保存到会话中?

发布于 2024-08-22 06:43:20 字数 433 浏览 4 评论 0原文

我在保存当前视图的状态时遇到一些问题。

目前,我有几个选择列表在控制器上调用它们自己的 Action 方法,该方法返回带有基于选择列表的值的过滤模型的 Index 视图。

我还编写了一些 FileResult 操作,用于根据当前模型创建 csv 文件。但我现在只介绍一个选择列表,因为我只将 selectList1 的值保存到会话中并使用 Session["SelectListValue1"] 访问它,

  • 这种情况下的最佳实践是什么?
  • 我应该重做整个(每个 SelectList 的每个操作)部分吗?
  • 我应该将每个 SelectLists 值保存到会话中并检查它是否为空吗?
  • 或者我应该将 Lambda 表达式保存到会话中并在每次调用期间修改它?

I am having some trouble saving the state of my current view.

Currenly I have several selectlist calling their own Action method on the controller that returns the Index view with the filtered model based on the values of the selectlist.

I have also written a little FileResult action that creates a csv file based on the current model. But I am only covering one selectlist right now as I only save the value of selectList1 into the session and access it with Session["SelectListValue1"]

  • What are the best practices in this situation?
  • Should I redo the entire (each action for each SelectList) part?
  • Should I save each SelectLists value into the session and check if it's null?
  • Or should I just save the Lambda Expression into the session and modify it during every call?

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

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

发布评论

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

评论(2

来世叙缘 2024-08-29 06:43:20

嗯,通常在 MVC 中我们不会直接保存到 Session,这不被认为是影响应用程序性能的最佳实践。一般来说,最佳实践是使每个请求尽可能无状态。

每个表单都应尽可能遵循 POST-Request-GET 模式,因此您通常不会像在 WebForms 中所做的那样(您不断发回相同的表单/操作)。

因此,您应该考虑您尝试捕获的状态代表什么。可能值的列表是一回事,可能从数据库中提取并存储为列表或可枚举在缓存中(在某些情况下;在其他情况下可以每次查找它)。不过,所选的值可能代表其他对象的属性,因此您应该使用它作为获取所选值的方法。

如果它不是持久对象的一部分,那么您可以每次读取发布值并再次设置视图状态(可能是最佳实践),或者,如果您需要在重定向中保留该值,则使用TempData 包(其工作方式与会话非常相似;实际上在幕后使用会话),但值会在下一个请求后被垃圾收集,因此您不必太担心内存膨胀。

Well, generally in MVC we don't directly save to Session, it's not considered a best practice b/c of impact to your app's performance. Generally, it's a best practice to make each request as stateless as possible.

Each form should follow the POST-Request-GET pattern where possible, so you're not going to do what you did in WebForms as a rule (where you keep posting back to the same form/action).

So you should consider what the state is that you're trying to capture represents. THe list of possible values is one thing, drawn possibly from a database and stored as a list or enumerable in the cache perhaps (in some scenarios; could look it up every time in others). The value that's selected probably represents a property on osme other object, though, so you should use that as your means of getting out the selected value.

If it's something that's not a part of a persistent object, then you can either just read the post values each time and set the viewstate again (probably the best practice) or, if you need to persist that value across a redirect, then use the TempData bag (which works much like session; in fact uses session under the hood) but values get garbage collected after one the next request, so you don't have to worry as much about the memory bloat.

梦忆晨望 2024-08-29 06:43:20

听起来您根本不需要使用会话。您不能通过查询字符串或表单传递选择列表的值吗?

It doesn't sounds like you need to be using the session at all. Can't you pass the values of your select lists via the query string or in a form?

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