从基本页面在不同控制器之间保留数据

发布于 2024-12-08 15:51:55 字数 609 浏览 0 评论 0原文

我不确定我在这里问的问题是否正确。

我有一个共享页面(母版页),它调用侧面菜单、页眉、页脚等的几个部分页面。并且我的所有控制器都继承 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苹果你个爱泡泡 2024-12-15 15:51:55

您仍然可以使用 HttpContext.Items,但需要通过 HttpContextBase 实例访问它。

为了向后兼容,您可以将 HttpContext 包装在 HttpContextWrapper 中,就像

var context = new HttpContextWrapper(HttpContext.Current);

@iamserious 的 上面的答案建议使用静态属性 - 这我强烈不同意。设置静态变量是应用程序范围内的,这意味着每个用户都将使用相同的变量 - 因此所有人都将具有相同的登录数据。您希望将其存储在Session中的每个用户或通过HttpContext.Items存储在每个Request中。


我建议使用类似这种方法,然后无论您在哪里调用 ContextStash.GetInstance,您都会在同一请求的生命周期内收到相同的实例。您还可以遵循相同的模式并使用 HttpContext.Session 而不是 HttpContext.Items

// could use this.HttpContext inside a controller, 
// or this.Context inside a view, 
// or simply HttpContext.Current
var stash = ContextStash.GetInstance(this.HttpContext);

if(!stash.IsSomething)
{
    // do something to populate stash.IsSomething
}

// class
public class ContextStash
{
    const string cacheKey = "ContextStash";

    public ContextStash(HttpContextBase context)
    {
        // do something with context
    }

    // your shared properties
    public bool IsSomething { get; set; }
    public string Foo { get; set; }
    public int Bar { get; set; }

    // instance methods
    public static ContextStash GetInstance()
    {
        return GetInstance(new HttpContextWrapper(HttpContext.Current));
    }

    public static ContextStash GetInstance(HttpContext context)
    {
        return GetInstance(new HttpContextWrapper( context ));
    }

    public static ContextStash GetInstance(HttpContextBase context)
    {
        ContextStash instance = context.Items[cacheKey] as ContextStash;
        if(null == instance)
        {
            context.Items[cacheKey] = instance = new ContextStash(context);
        }
        return instance;
    }
}

You can still use HttpContext.Items, but you'll need to access it via a HttpContextBase instance.

For backwards compatibility you can wrap an HttpContext in an HttpContextWrapper, like so

var context = new HttpContextWrapper(HttpContext.Current);

@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 per Request via HttpContext.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 of HttpContext.Items:

// could use this.HttpContext inside a controller, 
// or this.Context inside a view, 
// or simply HttpContext.Current
var stash = ContextStash.GetInstance(this.HttpContext);

if(!stash.IsSomething)
{
    // do something to populate stash.IsSomething
}

// class
public class ContextStash
{
    const string cacheKey = "ContextStash";

    public ContextStash(HttpContextBase context)
    {
        // do something with context
    }

    // your shared properties
    public bool IsSomething { get; set; }
    public string Foo { get; set; }
    public int Bar { get; set; }

    // instance methods
    public static ContextStash GetInstance()
    {
        return GetInstance(new HttpContextWrapper(HttpContext.Current));
    }

    public static ContextStash GetInstance(HttpContext context)
    {
        return GetInstance(new HttpContextWrapper( context ));
    }

    public static ContextStash GetInstance(HttpContextBase context)
    {
        ContextStash instance = context.Items[cacheKey] as ContextStash;
        if(null == instance)
        {
            context.Items[cacheKey] = instance = new ContextStash(context);
        }
        return instance;
    }
}
霓裳挽歌倾城醉 2024-12-15 15:51:55

好吧,如果您只想跨 BaseController 的多个实例使用一个变量,请使用 static 关键字,如下所示:

public class BaseController : Controller
{
    private static bool isUserAuthenticated;
}

现在,无论您有多少个 BaseController 实例,它们都将共享一个 isUserAuthenticated 变量,您可以更改以下值一,你彻底改变了它。

这是大多数面向对象编程的基础,如果您不介意我说的话,您确实应该花一些时间来了解 OOP 的概念。

well, if you just want to one variable across several instances of BaseController, use the static keyword, like so:

public class BaseController : Controller
{
    private static bool isUserAuthenticated;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文