问题描述
我试着用StructureMap来初始化我ValuesController从ApiController衍生物,但我发现一个例外,说:
Im trying to use StructureMap to initialize my ValuesController that derivate from ApiController but i'm getting an exception that says:
在IControllerFactory...... CustomControllerFactory未返回控制器的名称API。
The IControllerFactory '...CustomControllerFactory' did not return a controller for the name 'api'.
下面是code ..
Here is the code..
public class CustomControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
return (Controller)ConcretizationsManager.GetInstance(controllerType);
}
}
基本上ConcretizationsManager是StructureMap的包装..此方法始终运行确定为控制器的类型控制器,但APIController不。
basically ConcretizationsManager is a wrapper of StructureMap..This method always runs ok for Controllers of Controller type, but for APIController dont.
任何人都让我正确的方向?非常感谢
Anyone to get me to the right direction?Very thanks
推荐答案
有关我使用自定义的网络API的 ServiceActivator 的这样的(即不同的工厂则对于MVC):
For the Web API I am using Custom ServiceActivator like this (i.e. different factory then for MVC):
public class ServiceActivator : IHttpControllerActivator
{
public ServiceActivator(HttpConfiguration configuration) {}
public IHttpController Create(HttpRequestMessage request
, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = Factory.CreateInstance(controllerType) as IHttpController;
return controller;
}
}
其中,厂是我的Sturcturemap API的封装的
而在 App_Start \ WebApiConfig.cs
我做登记,像这样:
And in the App_Start\WebApiConfig.cs
I do register that like this:
config.Services.Replace(typeof(IHttpControllerActivator), new ServiceActivator(config));
这篇关于的WebAPI + APIController与structureMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!