我在VS2013下有一个OData V3 C#项目,我想在运行时在一个单独的类库中加载/注入一个ODataController,并让该服务成为路由。问题是我无法获得解析控制器的路径。例如http://localhost/odata/Tests
"No type was found that matches the controller named 'TestsController'"
这是我的控制器在单独的类库中的样子:
namespace WebApi.Extensions
{
public class TestsController : ODataController
{
var t = "some string";
public IHttpActionResult Get(ODataQueryOptions<Test> queryOptions)
{
...
}
}
}
在我的WebApi Register()方法中,我有这个:
public static void Register(HttpConfiguration config)
{
var builder = new ODataConventionModelBuilder();
<lots of 'local' builder.EntitySet<***>("***") calls>
builder.EntitySet<Test>("Tests");
var assembly = Assembly.LoadFrom("<full path to classlib.dll>");
// test that we can find and load an instance
var type = assembly.GetType("WebApi.Extensions.TestsController");
var obj = Activator.CreateInstance(type);
// no errors so far. I can 'see' into 'obj'
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel() );
}
任何人都可以看到为什么我无法获得解析到控制器类的路径?我错过了将它挂在一起的东西吗?
答案 0 :(得分:0)
我找到了我所缺少的东西。
Customizing controller discovery in ASP.Net
你基本上编写自己的并注册一个自定义的DefaultAssembliesResolver,你可以在其中获取从bin目录中自动解除的程序集列表(如果在IIS下托管),并从你想要的任何目录中添加你自己的程序集。
注意:上面链接中发布的源代码中有一个错误。浏览博客评论。