0

I'm getting a No parameterles constructor defined for this object error in my HomeController.cs. According to ASP.NET MVC: No parameterless constructor defined for this object I have to make sure that my ActionResults don't have a parameter. But my HomeController ActionResult doesn't have a parameter:

public class HomeController : Controller
{
    /// <summary>
    /// injected settings
    /// </summary>
    private ISettings settings;

    /// <summary>
    /// Initializes a new instance of the <see cref="HomeController"/> class.
    /// </summary>
    /// <param name="settings">The settings.</param>
    public HomeController(ISettings settings)
    {
        this.settings = settings;
    }

    /// <summary>
    /// Default index page
    /// </summary>
    /// <returns>redirects to the default application home page</returns>
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home", new { area = settings.Application, master = "default" });
    }
}

How can I find out where the error comes from?

Full stacktrace:

[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

[InvalidOperationException: An error occurred when trying to create a controller of type 'Web.Lifescience.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970356
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Community
  • 1
  • 1
Niek de Klein
  • 8,524
  • 20
  • 72
  • 143

1 Answers1

2

Your problem isn't connected with ActionResult. Your HomeController doesn't have parameterless constructor.
If you use controller's constructor with parameters, you should create your custom controller factory or use dependency injection: manually or with IoC-container according to your logic.

Community
  • 1
  • 1
Smileek
  • 2,702
  • 23
  • 26
  • Adding public HomeController() : this(new ISettings()) { } as suggested by the manually post gives doesn't work because I can't create an instance of an abstract class or interface (ISettings) – Niek de Klein Jun 17 '12 at 13:26
  • @Niek de Klein, of course you should pass some implementation of `ISettings`. What do you pass as a parameter in your program? – Smileek Jun 17 '12 at 13:31
  • I edited it in my post, the code is from HomeController instead of what I had before. – Niek de Klein Jun 17 '12 at 13:35
  • @Niek de Klein, as far as I understood, you're trying to use some IoC-container (Autofac, Ninject, Windsor Castle, Unity or something). When you configure it correctly, it will automatically resolve `ISettings` as some class, that implements it. – Smileek Jun 17 '12 at 13:41