MVC WebApi not using AutofacWebApiDependencyResolver

后端 未结 2 848
北荒
北荒 2021-02-14 09:40

I have a mixed MVC 4 app, where some controllers are regular implementations of Controller and some controllers are implementations of ApiController.

2条回答
  •  执笔经年
    2021-02-14 10:13

    To setup Autofac for Web.API you need to do two things:

    1. Register the Autofac dependency resolver (in the App_Start):

      GlobalConfiguration.Configuration.DependencyResolver = 
          new AutofacWebApiDependencyResolver(container);
      
    2. And register your Api controllers in your container builder with the call:

      containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());
      

提交回复
热议问题