Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

c# - How to configure Web Api 2 to look for Controllers in a separate project? (just like I used to do in Web Api)

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.

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.

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

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.

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");

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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?

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...