如何配置网页API 2查找控制器在一个单独的项目呢? (就像我以前在网页API做) [英] How to configure Web Api 2 to look for Controllers in a seperate project? (just like I used to do in Web Api)

查看:132
本文介绍了如何配置网页API 2查找控制器在一个单独的项目呢? (就像我以前在网页API做)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的控制器放置到MVC的网页API一个独立的类库项目。我用来添加下面一行在我的web API项目的Global.asax中寻找在单独的项目控制器:

I used to place my controllers into a separate Class Library project in Mvc Web Api. I used to add the following line in my web api project's global.asax to look for controllers in the seperate project:

ControllerBuilder.Current.DefaultNamespaces.Add("MyClassLibraryProject.Controllers");

我从来没有做任何其他的配置,除了加入上述行。这一直工作得很好,我。

I never had to do any other configuration, except for adding the above line. This has always worked fine for me.

但是我不能用上面的方法做同样的WebApi2。它只是不工作。该项目WebApi2仍然试图找到自己的项目的控制器文件夹中的控制器。

However I am unable to use the above method to do the same in WebApi2. It just doesn't works. The WebApi2 project still tries to find the controllers in its own project's controllers folder.

任何想法?

- 2个月后给予小小的总结更新(当我开始对这个富饶):

-- Giving little summary update after 2 months (As I started bounty on this):

我已经创建了一个WebApiOne的解决方案,它有2个项目,第一个是的WebAPI项目,第二个是一个类库,用于控制器。如果我添加引用到控制器类库项目进入的WebAPI项目,预期所有的作品。也就是说,如果我去 http://mydevdomain.com/api/values​​ 我可以看到正确的输出。

I have created a WebApiOne solution, it has 2 projects, the first one is WebApi project, and the second is a class library for controllers. If I add the reference to the controllers class library project into the WebApi project, all works as expected. i.e. if i go to http://mydevdomain.com/api/values i can see the correct output.

我现在已经创建了一个名为WebApiTwo的第二个项目,它有2个项目,第一个是WebApi2项目,第二个是一个类库,用于控制器。如果我的参考控制器类库项目添加到WebApi2项目,预期它行得工作。也就是说,如果我去 http://mydevdomain.com/api/values​​ 我得到没有类型被发现,命名控制器匹配'值'。

I have now create a second project called WebApiTwo, it has 2 projects, the first one is WebApi2 project, and the second is a class library for controllers. If I add the reference to the controllers class library project to the WebApi2 project, it doest NOT work as expected. i.e. if i go to http://mydevdomain.com/api/values i get "No type was found that matches the controller named 'values'."

对于第一个项目我没有做任何自定义设置在所有的,我没有:

for the first project i am not doing any custom settings at all, i do NOT have:

ControllerBuilder.Current.DefaultNamespaces.Add("MyClassLibraryProject.Controllers");

在我的Global.asax中,我还没有实现在他的两个博客文章由StrathWeb提出的任何定制的解决方案,我认为它不再适用;因为所有通过增加控制器项目的参考的WebAPI项目工程而已。

in my global.asax, and i have not implemented any custom solutions proposed by StrathWeb in two of his blog posts, as i think its not applicable any more; because all works just by adding the reference of the controller project to the WebApi project.

所以,我希望所有的工作同为WebApi2 ......但它不是。有没有人真正尝试过在WebAPi2 ???

So i would expect all to work same for WebApi2 ... but its not. Has any one really tried doing this in WebAPi2 ???

推荐答案

我刚刚确认这工作正常。检查事项:

I have just confirmed that this works fine. Things to check:

参考文献:请问你主要的Web API项目中引用外部类库

References: Does your main Web API project reference the external class library?

路由:让您设置可能与外部控制器干涉任何路由

Routing: Have you set up any routes that might interfere with the external controllers?

防护等级:在外部库控制器公共

继承:不要在外部库控制器从 ApiController

Inheritance: Do the controllers in the external library inherit from ApiController?

版本:使用Web API库相同版本的两个您的Web API项目和类库?

Versioning: Are both your Web API project and class library using the same version of the Web API libraries?

如果有帮助,我可以包我的测试解决方案,并提供给你。
此外,作为一个一点要注意,你并不需要告诉的Web API来找到您添加到的Global.asax 行控制器,系统会自动查找控制器只要你有他们引用。

If it helps, I can package up my test solution and make it available to you. Also, as a point to note, you don't need to tell Web API to find the controllers with the line you added to Global.asax, the system finds the controllers automatically provided you have them referenced.

这篇关于如何配置网页API 2查找控制器在一个单独的项目呢? (就像我以前在网页API做)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆