I ran into this problem when attempting to version one of the APIs that I'm working on.
I have 2 controllers that are named the same, are in different namespaces and are mapped to different routes. When I attempt to go to their different routes I get the error No HTTP resource was found that matches the request URI
.
Here's a code example:
namespace MvcApplication
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}
namespace MvcApplication
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
}
}
}
namespace MvcApplication.Controllers.v1
{
[RoutePrefix("api/v1/values")]
public class ValuesController : ApiController
{
[HttpGet]
[Route("")]
public IHttpActionResult GetValues()
{
return Ok(new string[] { "value1", "value2" });
}
}
}
namespace MvcApplication.Controllers.v2
{
[RoutePrefix("api/v2/values")]
public class ValuesController : ApiController
{
[HttpGet]
[Route("")]
public IHttpActionResult GetValues()
{
return Ok(new string[] { "value3", "value4" });
}
}
}
If I change the class names so that they aren't the same (such as ValuesControllerV1
and ValuesControllerV2
) everything works as expected.
Is there anyway to keep the same class names and have things resolve as expected?
EDIT: Updated to show initialization code as well.