Skip to main content
added di tag
Link
Gatogordo
  • 20.3k
  • 2
  • 39
  • 100
Source Link

SXA custom component looking for parameterless constructor

I am trying to create my first SXA compatible component, and I can't seem to get everything setup quite right. I decompiled the Maps component, and am using it as my guide. I understand that I need to use an MVC pattern, and so here's what I've got. Here's my controller:

public class OoyalaController : StandardController
{
    private readonly IOoyalaRepository _repository;
    public OoyalaController(IOoyalaRepository repository)
    {
        _repository = repository;
    }

    protected override object GetModel()
    {
        return _repository.GetModel();
    }
}

Here's my Interface:

public interface IOoyalaRepository : IModelRepository
{
}

And here's my repository class:

public class OoyalaComponentRepository: ModelRepository, IOoyalaRepository
{
    public override IRenderingModelBase GetModel()
    {
        OoyalaComponentModel model = new OoyalaComponentModel();
        FillBaseProperties(model);
        return model;
    }
}

Finally, here's my model class:

public class OoyalaComponentModel : RenderingModelBase
{
    public string MediaID { get; set; }
}

I've got my .cshtml looking like this:

@using Sitecore.XA.Foundation.SitecoreExtensions.Extensions
@model Components.Models.OoyalaComponentModel

<div class="component simple-component @Model.CssClasses.Aggregate()">
    <div class="component-content">
        <h1>You are on @Model.MediaID page</h1>
    </div>
</div>

In my content editor view, I believe I have the new rendering added to the right category and setup in the same way that the Map component is setup, as you can see in this screenshot: Ooyala Rendering

I also setup the Parameters template to include the IComponentVariant and Styling base templates: Parameters template

I seem to be missing something, however, as whenever I try to add my component to the page, I get the following error:

Exception: System.InvalidOperationException
Message: An error occurred when trying to create a controller of type 'Components.Controllers.OoyalaController'. Make sure that the controller has a parameterless public constructor.
Source: System.Web.Mvc
   at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)

Nested Exception

Exception: System.MissingMethodException
Message: No parameterless constructor defined for this object.
Source: mscorlib
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)

I understand that the system is looking for a parameterless constructor, however, the map component doesn't have a parameterless constructor.

What do I have configured/coded incorrectly?