简体   繁体   English

在MVC2控制器中维护状态

[英]Maintaining state in a MVC2 Controller

I want to maintain model state between method calls on the controller. 我想保持模型状态之间的方法控制器呼吁。
Is that possible? 那可能吗?

A controller is created at the call of some of its methods and destroyed when the called method finishes its execution? 控制器是在其某些方法的调用处创建的,并在被调用方法完成执行时销毁吗?

If not... how can I maintain the state between method calls? 如果没有,如何维护方法调用之间的状态? If that is possible... how has it to be written? 如果可能的话……该怎么写?

thanks 谢谢

Let's keep this simple! 让我们保持简单! :-) :-)

You asked following questions: 您问了以下问题:

I want to maintain model state between method calls on the controller. 我想在控制器的方法调用之间保持模型状态。

Is that possible? 那可能吗?

Answer: yes, it is 答: 是的

A controller is created at the call of some of its methods and destroyed when the called method finishes its execution? 控制器是在其某些方法的调用处创建的,并在被调用方法完成执行时销毁吗?

Answer: yes, it doesn't persist any state - it just returns the result generated by the action 答: 是的,它不会持久保存任何状态-它仅返回操作生成的结果

If not... how can I maintain the state between method calls? 如果没有,如何维护方法调用之间的状态? If that is possible... how has it to be written? 如果可能的话……该怎么写?

Answer: 回答:

We had the same issue in our current business application using ASP.NET MVC2 and came up with following solutions: 在使用ASP.NET MVC2的当前业务应用程序中,我们遇到了相同的问题,并提出了以下解决方案:

Solution(s): 解决方案:

1st he Proof-Of-Concept for this application contained some special functionality using the global Session . 首先,此应用程序的概念验证包含使用全局Session的一些特殊功能。 (Note that the session is saved on the webserver) (请注意,会话已保存在网络服务器上)

Saving and reading from session in ASP.NET MVC: 在ASP.NET MVC中保存和读取会话:

public Action SaveIntoSession()
{
  ...
  Session["SessionData"] = "Something to be stored in session";
  ...
}

public action ReadFromSession()
{
  ...
  // UNBOXING is required when you're using the session as ASP.NET doesn't know what is stored into the session
  string sessionData = (string)Session["SessionData"]; 
 ...
}

2nd There's another concept of saving the required information for each request into a Session Table into your database. 第二个概念是将每个请求的必需信息保存到会话表中的数据库中。 (But I won't go too deep into that.. check this article for more info - although it's php, you can get your head around the concept pretty easily) (但我不会对此进行深入探讨。请查看本文以获取更多信息-尽管它是php,但您可以很容易地了解这个概念)

3rd Would be to use the TempDataDictionary as mentioned by Charlino. 第三将使用Charlino提到的TempDataDictionary。 The problem with this is, that it only persists the state from one call to the another. 这样做的问题是,它只保留一个调用到另一个调用的状态。 All data that is stored will be deleted by the next request. 下一个请求将删除所有存储的数据。 (TempData is mostly used to display errors to the user..) (TempData通常用于向用户显示错误。)

public Action SaveIntoTempData()
{
  ...
  TempData["TempData"] = "Something to be stored ONE request ONLY";
  ...
}

public Action ReadFromTempData()
{
   ...
   string tempData = (string)TempData["TempData"];
}

4th You could also use the ViewDataDictionary . 4th您也可以使用ViewDataDictionary It is not recommended to be used with sensitive data. 不建议与敏感数据一起使用。 (check this thread for more info) (检查此线程以获取更多信息)

public Action SaveIntoViewData()
{
  ...
  TempData["ViewData"] = "Something to be stored into the page";
  ...
}

public Action ReadFromViewData()
{
   ...
   string viewData = (string)ViewData["ViewData"];
}

At the end of the day. 在一天结束时。 It is up to you and your team what best matches requirements. 最适合您的需求取决于您和您的团队。

You have access to the standard ASP.NET session, even in MVC. 您甚至可以在MVC中访问标准ASP.NET会话。 Check the docs . 检查文档

Would TempData / TempDataDictionary be suitable? TempData / TempDataDictionary是否合适?

HTHs, HTH,
Charles 查尔斯

I think by default you should be trying for a Restfull approach. 我认为默认情况下,您应该尝试Restrest方法。 If that is not viable then either serialise to the session object or store in a Singleton or something like that. 如果那不可行,则序列化到会话对象或存储在Singleton中或类似的东西。

By default I don't think MVC maintains state between calls. 默认情况下,我认为MVC不会在调用之间保持状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM