如本文所述,我将 ASP.NET MVC 4 Web API 项目配置为使用 StructureMap 2.6.2.0 ,但尽管显式调用,但访问/api/parts
仍返回以下错误:StructuremapMvc.Start();
Application_Start()
{
"ExceptionType": "System.ArgumentException",
"Message": "Type 'MyProject.Web.Controllers.PartsController' does not have
a default constructor",
"StackTrace": " at System.Linq.Expressions.Expression.New(Type type)\r\n
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}
我实现IDependencyResolver
并IDependencyScope
设置了 Web API 依赖解析器,如下所示~/App_Start/StructureMapMvc.cs
:
GlobalConfiguration.Configuration.DependencyResolver =
new StructureMapHttpDependencyResolver(container);
为什么 Web API 仍然在抱怨默认的构造函数?