asp.net-mvc-3 – 在使用Unity容器时为此对象异常定义的无参数构造函数
我得到上述异常,而trynig加载视图. 我正在使用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(); }} 这是我的错误堆栈. 没有为此对象定义的无参数构造函数.
有人可以帮我吗 谢谢. 解决方法ASP.NET MVC和ASP.NET Web API使用两个独立的依赖解析器.对于从Controller得到的“常规”MVC控制器,您需要使用DependencyResolver.SetResolver: DependencyResolver.SetResolver(new UnityDependencyResolver(container)); 对于由ApiController派生的Wep API控制器,您需要像代码中一样使用GlobalConfiguration.Configuration.DependencyResolver. 所以如果你打算使用这两种类型的控制器,你需要注册你的容器两次. 有一个很好的文章如何为依赖解析器设置Unity: Dependency Injection in ASP.NET MVC 4 and WebAPI using Unity (编辑:鲜蔬坊站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET微信公众号用于给指定OpenId用户发送红包
- asp.net-mvc – LINQ to SQL分页和COUNT(*)
- asp.net – 多线程环境中的文件访问策略(Web App)
- asp.net 无刷新分页实例代码
- asp.net-mvc – 在ajax请求中返回PDF
- asp.net-web-api – WebAPI:403在发布网站后被禁止
- asp.net-mvc – ASP.NET MVC模型/ ViewModel验证
- asp.net-mvc-3 – 应该如何看待“分离”?
- asp.net-mvc – LiveReload ASP.net MVC Web应用程序与Grun
- ASP.NET中读取XML文件信息的4种方法与示例代码
- 如何合理地构建我的ASP.NET MVC 2项目与区域
- asp.net – 启用Application Insights会使Web应用
- asp.net-mvc-3 – 为什么两个类,视图模型和域模型
- ASP.NET -- WebForm -- HttpResponse 类的方法和
- Asp.net配合easyui实现返回json数据实例
- asp.net-mvc – 更改ASP.NET MVC筛选器中的视图
- asp.net – 有没有比升级到Visual Studio 2010 U
- asp.net core 实现一个简单的仓储的方法
- asp.net – 为不同项目中的所有Web应用程序网页添
- asp.net – 请求在IIS工作进程中存在于RequestAc