I’m new to MVC but I’m trying to create an MVC Web API and I’m having a problem with routing.
I’m trying to work with the following URL patterns:
/api/venue/1999 // doesn't work
/api/venue/1999/images/ // works
The second URL pattern works fine, but if I try and access the first one, I get an error which says:
Multiple actions were found that match the request:
VenuefinderWebAPI.Models.Venue Get(Int32) on type
VenuefinderWebAPI.Controllers.VenueController System.String[]
Images(Int32) on type VenuefinderWebAPI.Controllers.VenueController
I’m not sure if it’s my routing which is wrong (multiple matches) or something in my controller class. How can fix this?
In my webApiConfig I have this:
config.Routes.MapHttpRoute(
name: "VenueImages",
routeTemplate: "api/venue/{venueID}/images/",
defaults: new { controller="Venue", action = "images" }
);
config.Routes.MapHttpRoute(
name: "VenueDefault",
routeTemplate: "api/venue/{venueID}",
defaults: new { controller = "Venue" }
);
VenueController.cs has:
// GET api/venue/5
public Venue Get(int venueID)
{
// code
}
and
[HttpGet]
[ActionName("images")]
string[] Images(int venueID)
{
// code
}
Try this…
Change your Venue controller:
Change your routes:
*Added Mike’s constraint to the route.
— What the above does —
The route matches urls, where the venueID is a number and the action method defaults to get method unless specified otherwise.
Remember that action methods are your methods in your controller.