如何在MVC应用程序的各种控制器之间共享公共数据结构 [英] how to share a common data structure among various controller of an MVC application
问题描述
我有一个带有多个控制器和视图的应用程序。当用户浏览各种视觉和控制器时,我需要记录导航历史以用于后向跟踪目的。为此,当我从控制器移动到控制器时,我决定使用堆栈来推送和弹出导航历史记录。问题是尝试了很多东西,比如
带有静态
属性的静态类,
HttpContext.Applicaion
属性,
HttpContext.Cache
property,
以及 HttpContext.Current.Session
属性
但每次我从控制器移动到控制器时这些属性都会被取消,并且在每种情况下都会创建一个新实例。(从控制器到控制器,我使用 RedirectToAction()
方法)
我需要的是一个可在整个应用程序中使用的堆栈,我可以从不同的控制器使用它来获取或设置导航信息。
I have an application with several controllers and views. As a user navigates through various vies and controller i need to record the navigation history for back tracking purpose. for this purpose i have decided to use a stack for pushing and popping the navigation history as i move from controller to controller. the problem is after trying a lot of stuff like
static class with static
properties,
HttpContext.Applicaion
property,
HttpContext.Cache
property,
and also the HttpContext.Current.Session
property
but each time i move from controller to controller these properties are nullified and a new instance is created in each case.(for going from controller to controller I am using RedirectToAction()
method)
What i need is an stack that is available throughout the application and i can use it from different controllers to get or set the navigation info.
推荐答案
这是什么会话状态是为了。只有在禁用会话处理或从构造函数访问时, HttpContext.Current.Session
才为空。
This is what session state is made for. The HttpContext.Current.Session
is null only if session handling is disabled, or when accessed from constructor.
这篇关于如何在MVC应用程序的各种控制器之间共享公共数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!