在App_Start中
IContainer container = SetupDependencyInjection();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
和
private IContainer SetupDependencyInjection()
{
var builder = new ContainerBuilder();
builder.RegisterApiControllers(typeof(WebApiApplication).Assembly);
builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
IContainer container = builder.Build();
return container;
}
这样做很好,但有时会显示错误
Error occurred when trying to create a controller of type 'ControllerName'. Make sure that the controller has a parameterless public constructor.
我是Web Api的新手。请帮帮我。