The purpose of the SessionState is to persist data about the user's session from one HttpRequest to the next. In some cases this is an an easier solution than manually implementing cookies or creating a data store for session info and passing an identifier. MVC's way of doing this is to use TempData, which wraps SessionState. This means it is probably unnecessary to directly access the SessionState unless you are overriding some of the core infrastructure. I think of a Model as being a more metaphorical representation of data. Using session-data in the Model wouldn't seem entirely right, because the model is probably representing some business logic, that SessionState would unnecessarily complicate. System.Web.Mvc provides a lot of mechanisms that allow us to maintain state without having to use the HttpContext directly. These mechanism are contained as properties of the ControllerContext, the ActionFilter filtering contexts, the ExceptionContext, the AuthorizationContext, the ModelBindingContext, RouteData and ViewContext. They help to separate the bunch of different things that one might store in a session state collection in more logically separated compartments.
TempData is available from the ControllerContext and the ViewContext. This is your access point to session state functionality. If you want to control the handling and writing of this data, and aren't sure where, action filters are a pretty good injection point, because they keep that concern out of the primary business-oriented controller action. This article explores the use of an ActionFilter that automatically persists the model state across requests: http://blog.jorritsalverda.nl/2010/03/10/maintainable-mvc-post-redirect-get-pattern/. I think it may be helpful. The question was not terribly specific... If I did not directly address your question please clarify.
会话状态实际上是一个 Web 客户端概念,为了分层,我更喜欢将它们排除在我的模型之外。或者至少我会将其包装在我自己的会话或上下文接口中。
I prefer keeping them at the Controller level or to hide them in custom Action Filters or model binders.
Session state is really a web client concept, I prefer to keep them out of my model for layering sake. Or at minimum I'd wrap it in my own Session or context interface.
发布评论
评论(2)
SessionState 的目的是将有关用户会话的数据从一个 HttpRequest 保存到下一个 HttpRequest。在某些情况下,这是比手动实现 cookie 或为会话信息创建数据存储并传递标识符更简单的解决方案。 MVC 执行此操作的方法是使用 TempData,它包装了 SessionState。这意味着可能没有必要直接访问 SessionState ,除非您要覆盖某些核心基础设施。我认为模型是数据的一种更具隐喻性的表示。在模型中使用会话数据似乎并不完全正确,因为模型可能代表一些业务逻辑,而 SessionState 会不必要地复杂化。 System.Web.Mvc提供了很多机制,允许我们维护状态而不必直接使用HttpContext。这些机制作为
ControllerContext
、ActionFilter
过滤上下文、ExceptionContext
、AuthorizationContext
、ModelBindingContext
、RouteData
和ViewContext
。它们有助于将会话状态集合中存储的一堆不同的东西分开,这些东西在逻辑上更独立。TempData 可从 ControllerContext 和 ViewContext 获取。这是您对会话状态功能的访问点。如果您想控制这些数据的处理和写入,并且不确定在哪里,操作过滤器是一个非常好的注入点,因为它们将这种关注排除在主要的面向业务的控制器操作之外。本文探讨了如何使用 ActionFilter 自动在请求之间保留模型状态:http://blog.jorritsalverda.nl/2010/03/10/maintainable-mvc-post-redirect-get-pattern/。我想这可能会有帮助。这个问题不是很具体......如果我没有直接解决你的问题,请澄清。
The purpose of the SessionState is to persist data about the user's session from one HttpRequest to the next. In some cases this is an an easier solution than manually implementing cookies or creating a data store for session info and passing an identifier. MVC's way of doing this is to use
TempData
, which wraps SessionState. This means it is probably unnecessary to directly access the SessionState unless you are overriding some of the core infrastructure. I think of a Model as being a more metaphorical representation of data. Using session-data in the Model wouldn't seem entirely right, because the model is probably representing some business logic, that SessionState would unnecessarily complicate. System.Web.Mvc provides a lot of mechanisms that allow us to maintain state without having to use the HttpContext directly. These mechanism are contained as properties of theControllerContext
, theActionFilter
filtering contexts, theExceptionContext
, theAuthorizationContext
, theModelBindingContext
,RouteData
andViewContext
. They help to separate the bunch of different things that one might store in a session state collection in more logically separated compartments.TempData is available from the ControllerContext and the ViewContext. This is your access point to session state functionality. If you want to control the handling and writing of this data, and aren't sure where, action filters are a pretty good injection point, because they keep that concern out of the primary business-oriented controller action. This article explores the use of an ActionFilter that automatically persists the model state across requests: http://blog.jorritsalverda.nl/2010/03/10/maintainable-mvc-post-redirect-get-pattern/. I think it may be helpful. The question was not terribly specific... If I did not directly address your question please clarify.
我更喜欢将它们保留在控制器级别或将它们隐藏在自定义操作过滤器或模型绑定器中。
会话状态实际上是一个 Web 客户端概念,为了分层,我更喜欢将它们排除在我的模型之外。或者至少我会将其包装在我自己的会话或上下文接口中。
I prefer keeping them at the Controller level or to hide them in custom Action Filters or model binders.
Session state is really a web client concept, I prefer to keep them out of my model for layering sake. Or at minimum I'd wrap it in my own Session or context interface.