No parameterless constructor defined for this object.


Hi, i am getting this issue while running a page in MVC, it was working fine previously, but i now i am getting the error as below

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: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[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, StackCrawlMark& stackMark) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +206
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +11
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

[InvalidOperationException: An error occurred when trying to create a controller of type 'QAWithGurus.Controllers.QuestionController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +76
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +88
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +194
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

How can i resolve it, the above Controller/page was working fine previously, also i would like to know the cause of the above issue.

thanks


Asked by:- Vipin
2
: 7300 At:- 8/13/2017 8:22:50 AM
C# asp-net No parameterless constructor







1 Answers
profileImage Answered by:- jaiprakash

as the error states "No parameterless constructor defined for this object", you don't have any constructor without parameters which can be used to initialize your controller, and load it

Suppose you are getting this issue in loading HomeController, then there must be a  parameter less constructor for it, something like

 public HomeController()
        {
           
        }

read this line from your error

InvalidOperationException: An error occurred when trying to create a controller of type 'QAWithGurus.Controllers.QuestionController'. Make sure that the controller has a parameterless public constructor.

if you will go through it, you will find it says "Make sure that the controller has a parameterless public constructor."

So that's it, you need to create a parameter less constructor in your controller, as stated above.

2
At:- 8/13/2017 12:53:49 PM
good answer and explanation 0
By : jaya - at :- 8/14/2017 3:10:04 PM
thank you, it worked 0
By : Vipin - at :- 8/16/2017 8:34:27 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use