My asp.net mvc 2.0 application is failing with the error "No parameterless constructor defined for this object"

I am new to asp.net mvc 2. I am trying to list all data from one table (ms sql server table). as the ORM I am using Entity Framework. now i am trying to write something for this:

Model:

    private uqsEntities _uqsEntity;
    public permissionRepository(uqsEntities uqsEntity)
    {
        _uqsEntity = uqsEntity;
    }

    public string getUserID()
    {
        return _uqsEntity.userPermissions.FirstOrDefault().ID.ToString();
    }

      

controller:

    private DataManager _dataManager;
    public HomeController(DataManager datamanager)
    {
        _dataManager = datamanager;

    }
    public ActionResult Index()
    {
        ViewData["Message"] = _dataManager.Permission.getUserID();

        return View();
    }

      

View

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>

      

when the app starts., my app. fails.

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

Exception Details: System.MissingMethodException: There is no unitless constructor defined for this object.

Source error:

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

Stack trace:

[MissingMethodException: No dimensionless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance (RuntimeType type, Boolean publicOnly, noCheck Boolean, Boolean & canBeCached, RuntimeMethodHandle & ctor, Boolean & bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow (Boolean publicOnly , Boolean fillCache) +86
System.RuntimeType.CreateInstanceImpl (Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
System.Activator.CreateInstance (type type, Boolean nonPublic) +67
System.Web.Mvc.Fateault.sextrolance ControllerType) +80

[InvalidOperationException: An error occurred while trying to create a controller of type 'Uqs.Controllers.HomeController. Make the controller have an unsigned public constructor.]
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance (RequestContext requestContext, ControllerType) +190 System.Web.Mvc.DefaultControllerFactory.CreateController (RequestContext requestContext) + String controller .MvcHandler.ProcessRequestInit (HttpContextBase httpContext, IController & controller, IControllerFactory & factory) +118
System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContextBase httpContext, callback AsyncCallback, condition of the object) +46
System.Web.Mvc.MvcHandler.BeginProcessRequest ( HttpContext httpContext, AsyncCallback Callback, Object State) +63
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest (HttpContext context, AsyncCallback cb, Object extraData) +13
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication. System.IExecution9. HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously) +155

Version Information: Microsoft .NET Framework Version: 2.0.50727.4200; ASP.NET Version: 2.0.50727.4016

please someone help me catch the problem.

Other classes: DataManager.cs

public class DataManager
 {
    private uqsEntities _entity;

    public DataManager(string connectionString)
    {
        this._entity = new uqsEntities(connectionString);
    }

    private permissionRepository _permissionsRepository;
    public permissionRepository Permission
    {
        get
        {
            if (_permissionsRepository == null)
                _permissionsRepository = new permissionRepository(_entity);
            return _permissionsRepository;
        }
    }
}

      

ControllerFactory.cs

public class ControllerFactory : DefaultControllerFactory
    {
        public Type controllerType { get; set; }
        public ControllerFactory()
        {

        }
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                base.GetControllerInstance(requestContext, controllerType);
            string connectionString = ConfigurationManager.ConnectionStrings["uqsEntities"].ConnectionString;
            return Activator.CreateInstance(controllerType, new DataManager(connectionString)) as IController;
        }
    }

      

+2


source to share


2 answers


I'll fix it, for example:



protected void Application_Start()
{
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
            //by this code:
            ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());
}

      

+3


source


Where is the parameterless constructor for your controller? ManagePermissionsController



The default factory controller requires one, so unless you create your own factory controller, you need to add it and change the way your DataManager is initialized.

+2


source







All Articles