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 ?
If your class library is built with
EF
then make sure you have the connection string specified in theApp.config
for the class library project, AND in theWeb.config
for your Web APIMVC
project.Apart from what has been said already:
Make sure you don't have two controllers of the same name in different namespaces.
Just had the case where one controller (foo.UserApiController) should be partially migrated to a new namespace (bar.UserApiController) and URI. The old controller was mapped by convention to /userapi, the new one was attribute-routed via
RoutePrefix["api/users"]
. The new controller didn't work until I renamed it to bar.UserFooApiController.Was running into same scenario and @justmara set me on the right path. Here's how to accomplish the force loading of the dependent assemblies from @justmara's answer:
1) Override the DefaultAssembliesResolver class
2) In the configuration section, replace the default with the new implementation
I cobbled this syntax together using pointers from this blog:
http://www.strathweb.com/2013/08/customizing-controller-discovery-in-asp-net-web-api/
As others have said, you know if you are running into this issue if you force the controller to load by directly referencing it. Another way is to example the results of
CurrentDomain.GetAssemblies()
and see if your assembly is in the list.Also: If you are self-hosting using OWIN components you WILL run into this. When testing keep in mind that the DefaultAssembliesResolver will NOT kick in until the first WebAPI request is submitted (it took me awhile to realize that).
You need to tell webapi/mvc to load your referrenced assembly. You do that with the compilation/assemblies section in your web.config.
Simple as that. You can do it with code the way @user1821052 suggested, but this web.config version will have the same effect.
It should work as is. Checklist
ApiController
ValuesController
Clean
the solution, manually removebin
folders and rebuildTemporary ASP.NET Files
folders. WebApi and MVC cache controller lookup resultThis controller:
matches this url:
http://localhost/MyValues/Get
(note there is no/api/
in route because it wasn't specified inRoutePrefix
.Controller lookup caching: This is default controller resolver. You will see in the source code that it caches lookup result.
When using AttributeRouting it is easily forgettable to decorate your methods with the
Route
Attribute, especially when you are using theRoutePrefix
Attribute on your controller class. It seems like your controller assembly wasn't picked up by the web api pipeline then.