Home » ASP » ASP.MVC » No parameterless constructor defined for this object

No parameterless constructor defined for this object

Few times ago, I open up a solution that was perfectly working previously and realized that this one compile but when I launch the browser raise me an exception :

No parameterless constructor defined for this object

.

parameterlessConstructor

Usually, this exception occur when you are using Asp.Net MVC with Unity and this one cannot resolve the parameters of your constructor. This mean that you request a controller method but the parameters aren’t provided. To solve this problem, usually people that develop with Asp.Net MVC and Unity use the Unity.MVC3 code that let you set the Unity dependency resolver to the framework that will resolve controller’s parameters when Asp.Net do a http request. The second option is to have constructor without parameter, which is often not possible.

But, in my scenario, I was using Unity.MVC3 and it was working before. The problem reside in the Web.Config file. Visual Studio has inserted an attribute that cause all the chaos of this exception.

The web.config has an attribute called “xmlns” with the value “http://schemas.microsoft.com/.NetConfiguration/v2.0” for the element configuration.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
   ...

By removing the xmlns attribute (and its value) everything worked as before. No more exception “No parameterless constructor defined for this object”, no more problem. So, if you have a parameterless constructor exception when using Asp.Net MVC and Unity, check out your configuration file to be sure that this attribute is not set.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

2 Responses so far.

  1. norman says:

    Simple fix is add default constructor that takes no parameter.
    (If any needed, just read from web.config)

    • Sure but the point is to use the correct constructor and not to by-pass the architecture. If the file require objects from the constructor, a technology choice should not change your way to instance your class.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.