i’m new in asp.net mvc 2.
I’m trying to list all data from one table(ms sql server table). as ORM I use Entity Framework. now, I’m tried to write something to do 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 I run app. , my app. fails.
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,
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.DefaultControllerFactory.GetControllerInstance(RequestContext
requestContext, Type controllerType)
+80[InvalidOperationException: An error
occurred when trying to create a
controller of type
‘uqs.Controllers.HomeController’. Make
sure that the controller has a
parameterless public constructor.]
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext
requestContext, Type controllerType)
+190 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext
requestContext, String controllerName)
+68 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase
httpContext, IController& controller,
IControllerFactory& factory) +118
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase
httpContext, AsyncCallback callback,
Object state) +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.IExecutionStep.Execute()
+8679186 System.Web.HttpApplication.ExecuteStep(IExecutionStep
step, Boolean& completedSynchronously)
+155Version Information: Microsoft .NET
Framework Version:2.0.50727.4200;
ASP.NET Version:2.0.50727.4016
please, somebody, help me to catch 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;
}
}
I fix it, like this: