如何在ASP.NET中跨实体请求保存状态而不使用EntityFramework保存到数据库? [英] How to save state across requests for an entity in ASP.NET without saving to database using EntityFramework?

查看:56
本文介绍了如何在ASP.NET中跨实体请求保存状态而不使用EntityFramework保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个CRUD ASP.NET WebForms Web应用程序,该应用程序由几个页面组成,用户可以在其中填充数据。在用户单击最后一页上的完成之前(由于各种原因),我的客户不想在页面之间的数据库中存储实体。有哪些选项可在页面之间传播填充的数据,哪一个最坏?从我的阅读中,我看到可以使用ViewState和Server.Transfer。还有其他选择,最好使用较少的魔术字符串和更多类型安全的数据绑定到实体对象?

I'm working on a CRUD ASP.NET WebForms web application consisting of a couple of pages in which the user fills in data. My client doesn't want to store the entities in the database between pages until the user clicks Finish on the last page (for various reasons). What options are there to propagate the filled in data between the pages and which is the least bad? From my readings I've seen that ViewState and Server.Transfer can be used. Any other options, preferably using less magic strings and more type safe data binding to entity objects?

推荐答案

使用 ViewState 将大大增加您通过网络发送的数据量,因为所有 Viewstate 数据都被序列化为表单中的隐藏输入,因此当您添加对象时,HTTP请求响应将显着增长。

Using ViewState is going to significantly increase the amount of data your sending down the wire, as all Viewstate data is serialised into a hidden input in the form, so as you add objects your HTTP request-response is going to grow significantly.

这确实没有神奇的方法。如果您的数据不太复杂,我会将这些值存储在查询字符串中。如果您的对象变得越来越大并且想要保持类型安全,我会使用 Session ,但要记得自己清理一下!

There's no magic bullet in this really. If your data is not too complex I'd store the values in the query string. If your objects are getting complex and big and you want to maintain type safety I'd use Session, but remember to clean up after yourself!

另一种选择是使用MVC范例,并使用隐藏输入以自身形式存储值。这意味着,即使用户中途出错,也不必担心清理会话,但它也可以使查询字符串保持整洁。

a further option is to use the MVC paradigm and store the values in the form it's self using hidden inputs. This means you don't need to worry about clearing up your session if the user bugs out half way though but it also keeps your querystring clean.

认为这就是您的所有选择, querystring viewstate(不要这样做) session 隐藏变量

think thats all your options, querystring, viewstate(don't do it), session or hidden variables.

好,所以您必须对数据进行序列化,因此您无法持久保留上下文。这是不可序列化的,因此上面是您的选择:

Ok so you have to seraialise your data, so you cannot persist the context. this is not serialiseable so the above are your options:

它们每个都有正负,


  • Viewstate(无效但易于使用)

  • 查询字符串(有效但对于大型数据集不可行且不可编辑)

  • Session(添加服务器负载,需要清理,但只允许您将数据保留在服务器上)

  • 隐藏变量(从用户隐藏但比viewstate更有效,每个属性都需要很多隐藏输入)

  • Viewstate (inefficent but easy to use)
  • Querystring (efficent but impractical for large data sets and editable)
  • Session(adds server load and needs cleaning up but allows you to persist data on the server only)
  • hidden variables(hidden from user but more efficient than viewstate requires a lot of hidden inputs for each property)

请选择!

这篇关于如何在ASP.NET中跨实体请求保存状态而不使用EntityFramework保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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