3

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.

Bengel
  • 1,043
  • 7
  • 12
  • Are any of the routes registered in this case? Or do you get `No HTTP resource was found that matches the request URI` for both? – James Aug 14 '14 at 15:30
  • Question: is attribute routing enabled? – Andrea Scarcella Aug 14 '14 at 15:32
  • Yes, attribute routing is enabled. If I simply change the name of the classes everything works. – Bengel Aug 14 '14 at 17:07
  • @James The default routes were in place and were working fine. I've removed them all to fit this example more specifically. Even still simply changing the class names makes the routes work just fine. – Bengel Aug 14 '14 at 17:17
  • See this [answer](http://stackoverflow.com/a/15959448/2030565) – Jasen Aug 14 '14 at 18:15

1 Answers1

0

IN MVC you can use the MapRoute Overload that has a namespace argument. Something like this:

routes.MapRoute(
     "Route for v1", // Route name
     "areav1/{controller}/{action}/{id}", // URL with parameters
     new { controller = "Values", action = "GetValues", id = UrlParameter.Optional },           new string[] { "MvcApplication.Controllers.v1"}
);

routes.MapRoute(
     "Route for v2", // Route name
     "areav2/{controller}/{action}/{id}", // URL with parameters
     new { controller = "Values", action = "GetValues", id = UrlParameter.Optional },           new string[] { "MvcApplication.Controllers.v2"}
);

Unfortunatly, this is not yet implemented in WebApi.

Here is a thread about this problem specifically:

Oualid KTATA
  • 1,116
  • 10
  • 20