从基本页面在不同控制器之间保留数据
我不确定我在这里问的问题是否正确。
我有一个共享页面(母版页),它调用侧面菜单、页眉、页脚等的几个部分页面。并且我的所有控制器都继承 BaseController。
现在,根据用户登录状态,我需要在所有这些部分页面中显示不同的数据,我认为检查用户是否登录的最佳位置是 BaseController。
这就是我的问题。我需要联系我的一项网络服务来查看用户是否登录并获取一些相关数据(如果登录)。我只需执行一次此操作,并且由于所有控制器都继承自 BaseController,因此每个部分页面调用都会导致 Web 服务调用。
显然,我不能只粘贴一个私有布尔变量 isUserAuthenticated 并检查标志,因为每个控制器都会有一个基本控制器的新实例。
在传统的 ASP.NET 项目中,我会将这些内容放入 HttpContext.Current.Items[] 中并重复使用它,但我无法(以某种方式)在 MVC 中访问它。
我不能只是不从部分页面上的基页继承,因为它们也可以独立调用,而且我也需要知道用户登录状态。
仅调用一次函数,或者仅在一次调用期间存储 bool 值的最佳方法是什么? - 控制器之间可访问。
人们如何做到这一点? 谢谢,抱歉,我是 mvc 的新手!
I am not sure I am asking the right question here.
I have a shared page (master page) that calls a couple of partial pages for side menu, header, footer etc.. and all my controllers inherit a BaseController.
Now, depending on the user login status, I need to show different data in all those partial pages and I thought where is the best place to check whether a user is logged in or not - BaseController.
And therein lies my problem. I need to contact one of my web services to see if a user is logged in and get some relevant data if he is. I only need to do this once, and since all controllers inherit from BaseController, each of those partial page calls results in the web service call.
Obviously, I cannot just stick a private bool variable isUserAuthenticated
and check for flag, as, each controller will have a new instance of the base controller.
In traditional asp.net projects, I would put this stuff in HttpContext.Current.Items[]
and use re-use it but I cannot (somehow) access that in MVC.
I cannot just not inherit from basepage on partial pages as they can also be called independently and I need to know the user login status then too.
What is the best way to call a function just once, or, rather, store a bool value for the duration of one call only? - accessible between controlers..
How do people do this?
thanks, sorry, I'm a newbie to mvc!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仍然可以使用 HttpContext.Items,但需要通过 HttpContextBase 实例访问它。
为了向后兼容,您可以将
HttpContext
包装在HttpContextWrapper
中,就像@iamserious 的 上面的答案建议使用静态属性 - 这我强烈不同意。设置静态变量是应用程序范围内的,这意味着每个用户都将使用相同的变量 - 因此所有人都将具有相同的登录数据。您希望将其存储在
Session
中的每个用户或通过HttpContext.Items
存储在每个Request
中。我建议使用类似这种方法,然后无论您在哪里调用 ContextStash.GetInstance,您都会在同一请求的生命周期内收到相同的实例。您还可以遵循相同的模式并使用
HttpContext.Session
而不是HttpContext.Items
:You can still use
HttpContext.Items
, but you'll need to access it via aHttpContextBase
instance.For backwards compatibility you can wrap an
HttpContext
in anHttpContextWrapper
, like so@iamserious's answer above suggests using a static property - which I strongly disagree with. Setting a static variable is application wide and would mean each and every user would be using the same variable - so all would have the same login data. You want to store it either per user in
Session
or perRequest
viaHttpContext.Items
.I'd suggest doing something using like this approach, then no matter where you call ContextStash.GetInstance, you'll receive the same instance for the lifetime of the same request. You could also follow the same pattern and use
HttpContext.Session
instead ofHttpContext.Items
:好吧,如果您只想跨 BaseController 的多个实例使用一个变量,请使用 static 关键字,如下所示:
现在,无论您有多少个 BaseController 实例,它们都将共享一个
isUserAuthenticated
变量,您可以更改以下值一,你彻底改变了它。这是大多数面向对象编程的基础,如果您不介意我说的话,您确实应该花一些时间来了解 OOP 的概念。
well, if you just want to one variable across several instances of BaseController, use the static keyword, like so:
Now, no matter how many instances of BaseController you have, they all will share a single
isUserAuthenticated
variable, you change value in one, you change it in all.This is the very basic of most object oriented programming and you should really take some time out to go through the concepts of OOP, if you don't mind me saying.