如何配置Web Api 2在单独的项目中查找Controller? (就像我以前在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)

查看:129
本文介绍了如何配置Web Api 2在单独的项目中查找Controller? (就像我以前在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项目仍然尝试在其自己项目的controllers文件夹中查找控制器.

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 ,我会得到没有类型发现与名为值"的控制器匹配."

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在单独的项目中查找Controller? (就像我以前在Web Api中所做的一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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