How to set ViewBag properties for all Views without using a base class for Controllers?

后端 未结 8 2183
故里飘歌
故里飘歌 2020-11-28 01:21

In the past I\'ve stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controlle

8条回答
  •  爱一瞬间的悲伤
    2020-11-28 01:55

    Un-tried by me, but you might look at registering your views and then setting the view data during the activation process.

    Because views are registered on-the-fly, the registration syntax doesn't help you with connecting to the Activated event, so you'd need to set it up in a Module:

    class SetViewBagItemsModule : Module
    {
        protected override void AttachToComponentRegistration(
            IComponentRegistration registration,
            IComponentRegistry registry)
        {
            if (typeof(WebViewPage).IsAssignableFrom(registration.Activator.LimitType))
            {
                registration.Activated += (s, e) => {
                    ((WebViewPage)e.Instance).ViewBag.Global = "global";
                };
            }
        }
    }
    

    This might be one of those "only tool's a hammer"-type suggestions from me; there may be simpler MVC-enabled ways to get at it.

    Edit: Alternate, less code approach - just attach to the Controller

    public class SetViewBagItemsModule: Module
    {
        protected override void AttachToComponentRegistration(IComponentRegistry cr,
                                                          IComponentRegistration reg)
        {
            Type limitType = reg.Activator.LimitType;
            if (typeof(Controller).IsAssignableFrom(limitType))
            {
                registration.Activated += (s, e) =>
                {
                    dynamic viewBag = ((Controller)e.Instance).ViewBag;
                    viewBag.Config = e.Context.Resolve();
                    viewBag.Identity = e.Context.Resolve();
                };
            }
        }
    }
    

    Edit 2: Another approach that works directly from the controller registration code:

    builder.RegisterControllers(asm)
        .OnActivated(e => {
            dynamic viewBag = ((Controller)e.Instance).ViewBag;
            viewBag.Config = e.Context.Resolve();
            viewBag.Identity = e.Context.Resolve();
        });
    

提交回复
热议问题