Hudvoy
Hudvoy

Reputation: 4096

ASP.NET WebAPI2 hosted on IIS express loads controllers from 2 differents projects

I created a solution that includes many OWIN WebApi2 Projects. Two of those projects has a controller with the same route.

When I try to start the first project, I get this error message :

Multiple controller types were found that match the URL. 
This can happen if attribute routes on multiple controllers match the requested URL. 
The request has found the following matching controller types: 
Project1.WebApi.Controllers.Project1Controller Project2.WebApi.Controllers.Project2Controller

However, both projects use different namespaces and don't have any dependencies between them.

How come the project2's controllers get loaded with Project1's controllers?

Thanks

Upvotes: 0

Views: 80

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

However, both projects use different namespaces and don't have any dependencies between them.

This doesn't matter. At runtime, ASP.NET Web API will inspect all types in all referenced assemblies that are inheriting from ApiController and will consider them candidates. So even if you have 2 different controllers in 2 completely different projects, as long as they have route collision, this collision will manifest at runtime. You will need to fix your application so that you never have 2 controllers loaded into the runtime with the same routes. ANd when you think about it, this actually makes lots of sense: given a route, ASP.NET Web API would not know which one of the 2 controllers to invoke if they both define exactly the same route. And in this case this anomaly is detected at runtime as soon as the application is started so that the developer is informed about this problem as soon as possible.

Upvotes: 3

Related Questions