ASP.NET MVC: No parameterless constructor defined for this object

Viewed 296859
Server Error in '/' Application.
--------------------------------------------------------------------------------

No parameterless constructor defined for this object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error: 


Line 16:             HttpContext.Current.RewritePath(Request.ApplicationPath, false);
Line 17:             IHttpHandler httpHandler = new MvcHttpHandler();
Line 18:             httpHandler.ProcessRequest(HttpContext.Current);
Line 19:             HttpContext.Current.RewritePath(originalPath, false);
Line 20:         }

I was following Steven Sanderson's 'Pro ASP.NET MVC Framework' book. On page 132, in accordance with the author's recommendation, I downloaded the ASP.NET MVC Futures assembly, and added it to my MVC project. [Note: This could be a red herring.]

After this, I could no longer load my project. The above error stopped me cold.

My question is not, "Could you help me fix my code?"

Instead, I'd like to know more generally:

  • How should I troubleshoot this issue?
  • What should I be looking for?
  • What might the root cause be?

It seems like I should understand routing and controllers at a deeper level than I do now.

28 Answers

First video on http://tekpub.com/conferences/mvcconf

47:10 minutes in show the error and shows how to override the default ControllerFactory. I.e. to create structure map controller factory.

Basically, you are probably trying to implement dependency injection??

The problem is that is the interface dependency.

This type error may come up due to missing dependency injector/resolver container and/or missing the bindings

  1. Add Dependency injector to your project using NugetPacketManager (Unity, Ninject or whichever you like)
  2. Add binding(s) for interface and concrete implementation for the classes

UnityMConfig.cs

using System;
using Unity;
using <your_namespace for the interfaces and concrete classes>

namespace <your_namespace>
{
    public static class UnityConfig
    {
        private static Lazy<IUnityContainer> container =
            new Lazy<IUnityContainer>(() =>
            {
                var container = new UnityContainer();
                RegisterTypes(container);
                return container;
            });

        public static IUnityContainer Container => container.Value;

        public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType <IInterfaceClassName, ConcreteImplementationOfInterface> ();
        }
    }
}

UnityMvcActivator.cs

using System.Linq;
using System.Web.Mvc;
using Unity.AspNet.Mvc;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(xxxx.UnityMvcActivator), nameof(xxxx.UnityMvcActivator.Start))]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(xxxx.UnityMvcActivator), nameof(xxxx.UnityMvcActivator.Shutdown))]

namespace <your_namespace>
{
    public static class UnityMvcActivator
    {
        public static void Start()
        {
            FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(UnityConfig.Container));
            DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Container));
        }
        public static void Shutdown()
        {
            UnityConfig.Container.Dispose();
        }
    }
}

In my case, my class had the [Serializable] attribute.

You are required to have a constructor that takes no parameters if your class is [Serializable]

I added a parameterless constructor to the model inside of DOMAIN Folder, and the problem is solved.

enter image description here

 public User()
        {

        }

I got the same issue when trying to Add-Migration on the DbContext class.

First I got the error: Unable to create an object of type 'ContextDb'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Which made me add the interface IDesignTimeDbContextFactory and implement it's function:

public ContextDb CreateDbContext(string[] args)
    {
        var optionBuilder = new DbContextOptionsBuilder<ContextDb>();
        optionBuilder.UseSqlServer("Server_connection");
        return new ContextDb(optionBuilder.Options);
    }

At this time the error No parameterless constructor defined for type 'LibraryContext.ContextDb' happened when trying to Add-Migration again. Which occured due to the new instance of the ContextDb created in CreateDbContext(). To fix this I just added an empty constructor:

public ContextDb()     
    {
    }
Related