MS MVC - 我可以以某种方式全局填充我所有视图的强类型 ViewData/Model 吗?

发布于 2024-07-15 03:06:54 字数 501 浏览 13 评论 0原文

我想为我的 MVC 应用程序中的所有视图提供已登录用户的实例(如果尚未登录,则为 null)。 我已经成功实现了我自己的 ControllerActionInvoker 类,该类重写了 InvokeAction 并在 ViewData 中提供了记录的用户实例 (controllerContext.Controller.ViewData["LoggedUser"] = xxx)。 问题是我想使用强类型模型在整个应用程序中传递记录的用户实例。 我正在考虑拥有 ApplicationViewDataBase 类,它可以是我所有强类型 ViewData 类的基类,并且也可以提供记录的用户实例。 我可以轻松访问所有视图中记录的用户实例。

是否可以像我在 ControllerActionInvoker.InvokeAction 覆盖中实现的那样填写全局强类型? 或者以某种方式在 Page.User 中提供我的 User 实例是否更好? 我发现使用 Page.User 可能更灵活,但也没有找到任何解决方案如何注入我的 User 实例......

I'd like to supply instance of logged User (or null if nobody logged yet) for all views in my MVC application. I've already succesfully implemented my own ControllerActionInvoker class which overrides InvokeAction and supplies logged user instance in ViewData (controllerContext.Controller.ViewData["LoggedUser"] = xxx). The problem is that I'd like to use the strongly typed Model for passing the logged user instance in whole application. I was thinking about having ApplicationViewDataBase class which could be base class for all my strongly typed ViewData classes and which would provide logged user instance also. I could than easily access logged user instance in all my Views.

Is it possible to fill in the strongly typed globally like I achieved in ControllerActionInvoker.InvokeAction override? Or is it better to somehow supply my User instance in Page.User? I'd find probably more slick to use Page.User, but also didn't find any solution how to inject my User instance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

雨落星ぅ辰 2024-07-22 03:06:54

也许更好的方法是将 User 放入 HttpContext 中? 在 global.asax 中

protected void Application_AuthenticateRequest(object sender, EventArgs e)

Maybe better way to put User into the HttpContext? In global.asax

protected void Application_AuthenticateRequest(object sender, EventArgs e)

呆头 2024-07-22 03:06:54

这是一个老问题,因此为了将来的访问者的利益,可以通过创建从 Controller 继承的基本控制器并处理 OnActionExecuted() 以将自定义用户实例添加到控制器操作和视图之间的视图来完成。 这里有一个全面的实现:http: //blog.bitdiff.com/2012/05/sharing-common-view-model-data-in.html

如果您希望该数据可供您的操作方法使用,那么事情会变得更加复杂,因为视图还不存在。

对于 POST 方法,“baseView = filterContext.Controller.ViewData.Model as BaseView”将使您能够访问视图,但您需要一个自定义模型绑定器来设置 ViewData.Model(请参阅 https://stackoverflow.com/a/25250058/2381157)。 不幸的是,这不是一个全局解决方案,因为除非有东西要绑定,否则不会调用模型绑定器。

一种可能适合的方法是使用 ActionFilter 或 AttributeFilter 向 RouteData 或 ActionParameters 注入一个值,但您的操作必须将此值传输到视图。

我找不到一种干净、简单的方法来在创建视图时将这些数据注入到视图中,并回退到实现用户实例的线程安全缓存,以便我可以在需要时检索用户实例,而无需数据库查询高架。 这并不容易,但在保持交叉请求用户状态方面具有其他优势(这通常是您希望控制器逻辑可用的自定义用户数据的原因)。

This is an old question so just for the benefit of future visitors, this can be done by creating a basecontroller inherited from Controller and handling OnActionExecuted() to add your custom user instances to the view between the controller action and the view. There's a comprehensive implementation of that here: http://blog.bitdiff.com/2012/05/sharing-common-view-model-data-in.html

If you want that data to be available to your action method things get a bit more complicated as the view won't exist yet.

For POST methods "baseView = filterContext.Controller.ViewData.Model as BaseView" will get you access to the view but you'll need a custom modelbinder to set ViewData.Model (see https://stackoverflow.com/a/25250058/2381157). Unfortunately this isn't a global solution as the modelbinder isn't invoked unless there's something to bind.

One approach that might suit is an ActionFilter or AttributeFilter that injects a value to RouteData or ActionParameters but your action would have to transfer this value to the view.

I couldn't find a clean, simple way to inject this data to the view as it was created and fell back to implementing a thread-safe cache of user instances so that I could retrieve the user instance whenever it was needed without the database query overhead. That wasn't easy but had other advantages for keeping cross-request user state (which is generally why you'd want to have custom user data available to the controller logic).

愿得七秒忆 2024-07-22 03:06:54

你不需要添加这个; 它已经在 ASP.NET MVC 中可用。 查看ViewContext.HttpContext.User.Identity.Name

You don't need to add this; it's already available in ASP.NET MVC. Look at ViewContext.HttpContext.User.Identity.Name

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