asp.net mvc - How to set ViewBag properties for all Views without using a base class for Controllers? -


In the past I have seen common assets such as the current user being trapped, in a global fashion on view data / viewbacks

It has allowed me to use the IOC on the base controller and has not been able to share this kind of data globally.

I am wondering if there is an alternative way of incorporating such code into the MVC pipeline?

Un-attempted by me, but you can see the view data during the activation process and then watch it You can.

Because the views on the in-fly are registered, registration syntax does not help you connect to active events, so that you set it to module Up:

  class setViewBagItems module: module {attach without secure override computer registration (IComponentRegistration registration, IComponentRegistry registry) {if (typeof (WebViewPage) .IsAssignableFrom (registration.activator.lit type) ) {Registration. Active = = (S, E) => {(WebViewPage) e.Instance) .ViewBag.Global = "global"; }; }}}   

This may be one of "only a hammer of equipment" - types of suggestions by me;

Edit: Optional, Low Code Approach - Just Attach to Controller

  Public Category SetViewBagItems Module: Module {Protected Override Zero Enclosure Insertion Registration (IComponentRegistry CR, IComponentRegistration reg) {type limitType = reg.Activator.LimitType; If (typef (controller) .isAssignableFrom (limitType)) {Registration. Active = = (S, E) => {Dynamic ViewBag = ((Controller) E. Instance.) View Bag; ViewBag.Config = e.Context.Resolve & lt; Config & gt; (); ViewBag.Identity = e.Context.Resolve & lt; IIdentity & gt; (); }; }}}   

Edit 2: Another method which works directly with the administrator registration code:

  builder.RegisterControllers (Asm) .OnActivated (E = & gt; {dynamic viewBag = ((controller) e.Instance) .ViewBag; viewBag.Config = e.Context.Resolve & LT; config & gt; (); viewBag.Identity = e .Context.Resolve & lt; IIdentity & gt; ();});    

Comments