如何配置 Web Api 2 以在单独的项目中查找控制器?(就像我以前在 Web Api 中所做的那样) [英] How to configure Web Api 2 to look for Controllers in a separate project? (just like I used to do in Web Api)

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

问题描述

我曾经将我的控制器放在 Mvc Web 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 separate 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 work. 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 我得到没有类型是发现与名为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 anyone 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?

保护级别:控制器是否在外部库public中?

Protection Level: Are the controllers in the external library public?

继承:外部库中的控制器是否继承自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.

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

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