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.
All of the answers say to create a parameters less constructor which isn't ideal if you don't want any other devs using it and only the model binder.
The attribute
[Obsolete("For model binding only", true)]
above a public constructor will throw a compiler error if another dev tries to use this. Took me ages to find this, hope it helps someone.You need the action that corresponds to the controller to not have a parameter.
Looks like for the controller / action combination you have:
but you need
Also, check out Phil Haack's Route Debugger to troubleshoot routes.
I had same problem but later found adding any new interface and corresponding class requires it to be registered under Initializable Module for dependency injection. In my case it was inside code as follows:
}
By default, MVC Controllers require a default constructor with no parameters. The simplest would be to make a default constructor that calls the one with parameters:
However, you can override this functionality by rolling your own
ControllerFactory
. This way you can tell MVC that when you are creating aMyController
give it an instance ofHelper
.This allows you to use Dependency Injection frameworks with MVC, and really decouple everything. A good example of this is over at the StructureMap website. The whole quickstart is good, and he gets specific to MVC towards the bottom at "Auto Wiring".
While this may be obvious to some, the culprit of this error for me was my MVC method was binding to a model that contained a property of type
Tuple<>
.Tuple<>
has no parameterless constructor.The same for me. My problem appeared because i forgot that my base model class already has property with the name which was defined in the view.
... and controller processing model :
Nothing special, right? But then i define the view ...
... which causes "Parameterless constructor" error on POST request.
Hope that helps.