Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • nickornotto 317 posts 679 karma points
    Dec 17, 2020 @ 11:38
    nickornotto
    0

    How to register Umbraco RenderMvcControllers in glabal asax

    I am trying to use RenderMvcController and SurfaceController with dependency injection but it doesn't work.

    public class SupplierRenderMvcController : RenderMvcController
    {
        public HomeRenderMvcController(IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
    }
    
    public class SupplierController : SurfaceController
    {
        public SupplierController(IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
    }
    

    It is saying:

    No parameterless constructor defined for this object.

    Umbraco's controllers are registered in in Global.asax:

            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    

    I have api controllers using IoC with no problem in other projects of the same solution.

    The above controlers are in the main project of the solution though.

    This is my full Global.asax of the main project:

    public class MvcApplication : IApplicationEventHandler
    {
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            AreaRegistration.RegisterAllAreas();
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            RegisterIoCContainer();
        }
    
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        private void RegisterIoCContainer()
        {
            var builder = new ContainerBuilder();
    
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
    
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly‌​());
    
            builder.RegisterModule<WebApiConfig>();
    
            var container = builder.Build();
    
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        }
    }
    

    MembershipService is registered within WebApiConfig:

    public class WebApiConfig : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.AddMediatR(this.GetType().Assembly);
    
            builder.RegisterType<MembershipService1>().As<IMembershipService>();
        }
    }
    
  • Huw Reddick 274 posts 857 karma points
    Jan 04, 2021 @ 12:58
    Huw Reddick
    0

    is the error referring to this

    public class SupplierRenderMvcController : RenderMvcController
    {
        public HomeRenderMvcController(IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
    }
    

    Shouldn't the constructor be called SupplierRenderMvcController and not HomeRenderMvcController

  • Elyssaa 6 posts 76 karma points
    Jan 05, 2021 @ 06:11
    Elyssaa
    0

    You could always add global things such as Output Caching. public virtual ActionResult Index(RenderModel model) a new Global Application class where we can setup Simple Injector and register any interfaces and their implementations. We can do this by editing our global.asax file as follows.

    PupilPath

Please Sign in or register to post replies

Write your reply to:

Draft