Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1088085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:01:29+00:00 2026-05-16T23:01:29+00:00

Having the code below in my Global.asax.cs and two controller (one based on a

  • 0

Having the code below in my Global.asax.cs and two controller (one based on a the other: MasterController) I don’t seem to find how can I resolve the repository register in my WindsorContainer from the MasterController… the same applies in the HomeController and works perfectly… what am I doing wrong?

Global.asax.cs:

private IWindsorContainer _container;

protected void Application_Start()
{
    InitializeContainer();
    RegisterRoutes(RouteTable.Routes);
}

protected void Application_End()
{
    this._container.Dispose();
}

protected void Application_EndRequest()
{
    if (_container != null)
    {
        var contextManager = _container.Resolve<IContextManager>();
        contextManager.CleanupCurrent();
    }
}

private void InitializeContainer()
{
    _container = new WindsorContainer();

    ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));

    // Register context manager.
    _container.Register(
        Component.For<IContextManager>()
        .ImplementedBy<EFContextManager>()
        .LifeStyle.Singleton
        .Parameters(
            Parameter.ForKey("connectionString").Eq(ConfigurationManager.ConnectionStrings["ProvidersConnection"].ConnectionString)
        )
    );

        //Products repository           
    _container.Register(
        Component.For<IProductRepository>()
        .ImplementedBy<ProductRepository>()
        .LifeStyle.Singleton
    );

    // Register all MVC controllers
    _container.Register(AllTypes.Of<IController>()
        .FromAssembly(Assembly.GetExecutingAssembly())
        .Configure(c => c.LifeStyle.Transient)
    );

}

Controller base:

public class MasterController : Controller
{
    private IProductRepository _productRepository;

    public ProductController(IProductRepository product)
    {
        _productRepository = product;
    }

    public ActionResult Index()
    {
       ViewData["product"] = _productRepository.FindOne(123);   
       return View();
    }
}

Controller based on MasterController:

public class ProductController : MasterController
{
    private IProductRepository _productRepository;

    public ProductController(IProductRepository product)
    {
        _productRepository = product;
    }

    public ActionResult Search(int id)
    {
       ViewData["product"] = _productRepository.FindOne(id);    
       return View();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:01:30+00:00Added an answer on May 16, 2026 at 11:01 pm

    It is working as expected now and the ViewDatas are accessible from any controller/view.

    First I created a public class where I store my Windsor container so it can be accessed from any controller:

    public static class IOCcontainer
    {
        public static IWindsorContainer Container { get; set; }
    }
    

    Then in my global.asax.cs I have:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        InitializeContainer();
    }
    
    private void InitializeContainer()
    {
        _container = new WindsorContainer();
    
        // Register context manager.
        _container.Register(
            Component.For<IContextManager>()
            .ImplementedBy<EFContextManager>()
            .LifeStyle.Singleton
            .Parameters(
                Parameter.ForKey("connectionString").Eq(ConfigurationManager.ConnectionStrings["ProvidersConnection"].ConnectionString)
            )
        );
    
            //Products repository           
        _container.Register(
            Component.For<IProductRepository>()
            .ImplementedBy<ProductRepository>()
            .LifeStyle.Singleton
        );
    
        // Register all MVC controllers
        _container.Register(AllTypes.Of<IController>()
            .FromAssembly(Assembly.GetExecutingAssembly())
            .Configure(c => c.LifeStyle.Transient)
        );
    
        IOCcontainer.Container = _container; //set the container class with all the registrations
    
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
    }
    

    So now in my master controller I can use:

    public class MasterController : Controller
    {
    
        private IProductRepository g_productRepository;
    
        public MasterController() : this(null,null,null,null,null)
        {
        }
    
        public MasterController(IProductRepository productRepository)
        {
            g_productRepository = productRepository ?? IOCcontainer.Container.Resolve<IProductRepository>();
        }
    
        //I don't use an action here, this will make execute it for any action in any controller
        protected override void OnActionExecuting(ActionExecutingContext context)
        {   
            if (!(context.ActionDescriptor.ActionName.Equals("Index") && context.Controller.ToString().IndexOf("Home")>0)) {
            //I now can use this viewdata to populate a dropdownlist along the whole application
            ViewData["products"] = g_productRepository.GetProducts().ToList().SelectFromList(x => x.Id.ToString(), y => y.End.ToShortDateString());
            }
        }
    }
    

    Then the rest of controllers:

    //will be based on MasterController
    public class AboutController : MasterController 
    {
    
    }
    
    public ActionResult Index()
    {
        return View();
    }
    
    etc...
    

    Probably not the most elegant way to do it but it will do until I find a better way or someone else brighten my mind up!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.