Autofac 同时支持MVC 与Webapi
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Autofac 同时支持MVC 与Webapi相关的知识,希望对你有一定的参考价值。
1.引用
using Autofac; using Autofac.Integration.Mvc; using Autofac.Integration.WebApi;
2.在Global中的Application_Start方法中添加如下
// Get your HttpConfiguration. var config = GlobalConfiguration.Configuration; var builder = new ContainerBuilder(); //builder.RegisterType<Helper>().As<IHelper>().InstancePerLifetimeScope(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // OPTIONAL: Register the Autofac filter provider. builder.RegisterWebApiFilterProvider(config); var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
以上是关于Autofac 同时支持MVC 与Webapi的主要内容,如果未能解决你的问题,请参考以下文章
Autofc与Mvc,WebForm,Weiapi,Owin整合源码分析
autofac + owin + webform + mvc + webapi集成demo