我在尝试加载视图时遇到上述异常。
我正在使用 Unity 来初始化我的控制器实例。仍然收到上述错误。
这是我的控制器。
public class SiteController : Controller
{
private ISiteRepository _repository;
public SiteController(ISiteRepository repository)
{
_repository = repository;
}
//
// GET: /Site/
public ActionResult Index()
{
return View();
}
//
// GET: /Site/Details/5
public ActionResult Details(int id)
{
return View();
}}
这是我的 Global.asax.cs
protected void Application_Start()
{
ConfigApi(GlobalConfiguration.Configuration);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
static void ConfigApi(HttpConfiguration config)
{
var unity = new UnityContainer();
unity.RegisterType<SiteController>();
unity.RegisterType<ISiteRepository, SiteRepository>(new HierarchicalLifetimeManager());
config.DependencyResolver = new IocContainer(unity);
}
这是我的 SiteRepository 类。
public class SiteRepository:ISiteRepository
{
private readonly SampleMVCEntities _dbContext;
public SiteRepository()
{
_dbContext = new SampleMVCEntities();
}
private IQueryable<SiteConfig> MapSiteConfig()
{
return _dbContext.SiteConfigs.Select(a => new SiteConfig
{
Name = a.Name,
LinkColour = a.LinkColour,
SiteLogo = a.SiteLogo,
SiteBrands = a.SiteBrands.Select(b => new Models.SiteBrand { SiteId = b.SiteId, BrandId = b.BrandId })
});
}
public IEnumerable<SiteConfig> GetAll()
{
return MapSiteConfig().AsEnumerable();
}}
这是我的错误堆栈。
没有为此对象定义无参数构造函数。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。
源错误:
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪:
[MissingMethodException: 没有为此对象定义无参数构造函数。] 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) +114
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type ) +6 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55[InvalidOperationException:尝试创建类型为“Config.Controllers.SiteController”的控制器时发生错误。确保控制器有一个无参数的公共构造函数。]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +179
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System. Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +197 System.Web.Mvc.<> c_DisplayClass6.b_2() +49 System.Web.Mvc.<>c__DisplayClassb 1 func) +88 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) +98 System.Web.Mvc.MvcHandler.BeginProcessRequest( HttpContext httpContext,AsyncCallback 回调,对象状态)+50 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData)+16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication。 IExecutionStep.Execute() +268 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +1551.<ProcessInApplicationTrust>b__a() +13 System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func
有人可以帮助我吗?
谢谢。