开发者

Sessions in MVC [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 years ago.

Improve this question

Where does session h开发者_JS百科andling fit in the overall concept of MVC? The model or the controller?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜