Reputation: 2091
I created a new ASP.NET MVC4 Web Api Project. In addition to the default ValuesController
, I added another controller, ScenarioController
. It has the exact same methods as ValuesController
. But for some reason, it behaves differently.
/api/values/ => "value1","value2"
/api/values/1 => "value"
/api/scenario/ => "value1","value2"
/api/scenario/1 => "value1","value2"
^^^^^^^^^^^^^^^^^
should return "value"!
Using breakpoints, I know that /api/scenario/1
actually gets sent to the public IEnumerable<string> Get()
, not the expected public string Get(int id)
. Why?
For reference, here are the relevant files (these are pristine default mvc4-webapi classes, haven't modified anything):
Global.asax.cs
namespace RoutingTest
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
WebApiConfig.cs
namespace RoutingTest
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
}
ValuesController.cs
namespace RoutingTest.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
}
ScenarioController.cs (yes, it's in the Controllers folder)
namespace RoutingTest.Controllers
{
public class ScenarioController : ApiController
{
// GET api/scenario
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/scenario/5
public string Get(int id)
{
return "value";
}
}
}
Upvotes: 3
Views: 3018
Reputation: 1
I was having this issue and could not get anything to work. Finally I changed the port on the IIS Express Project Url setting and all is back to normal. It was localhost:57846. I just made it localhost:57847 and all is back to normal.
Upvotes: 0
Reputation: 700
I tried your code just now and got the expected result:
> curl http://localhost:53803/api/values
["value1","value2"]
> curl http://localhost:53803/api/values/1
"value"
> curl http://localhost:53803/api/scenario
["value1","value2"]
> curl http://localhost:53803/api/scenario/1
"value"
>
(By the way, there is no requirement that it be in the Controllers
folder. HttpConfiguration.Routes.MapHttpRoute
simply finds all your classes that inherit from ApiController
.)
I am not being sarcastic when I suggest that you Rebuild All and try again.
Upvotes: 2
Reputation: 2091
Gremlins. Thanks to @Pete Klien for verifying that the code does work outside my machine. Here's what I did.
Upvotes: 2