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 5中添加一个TypeScript绝对类型的定义?
- 从app_data中删除文件夹时如何防止asp.net重新编译?
- asp.net – “线程被中止了什么”. ‘SNIReadSync(SNI_Conn
- asp.net – @ Url.Action在控制器中创建空值的参数之间添加
- asp.net+ajaxfileupload.js 实现文件异步上传代码分享
- ASP.NET MVC全能路由
- asp.net-mvc – 找到相同类型的两个实体之间的差异
- asp.net-mvc – 如何在ASP.NET MVC中传递页面的元标记?
- asp.net-mvc-2 – 如何在ASP.NET MVC2中为Html.LabelFor()添
- asp.net 自动将汉字转换成拼音第一个字母
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- ASP.NET和System.Diagnostics跟踪 – 我错过了什
- .net – RESTful WCF的裸最低配置
- asp.net-core – 如何在Visual Studio 2015 RC中
- asp.net fileupload控件上传文件与多文件上传
- asp.net-core – 如何使用ASP.NET注册OData 5
- 如何在ASP.NET 5中添加一个TypeScript绝对类型的
- 并行运行ASP.NET Webforms和ASP.NET MVC
- asp.net – 在fileupload中选择立即调用C#函数文
- asp.net-mvc – 从控制器重定向初始化不工作
