Thousands of people seem to be asking the same question about stack overflows, but there seems to be no single solution to this problem. I'm going to ask him again ...
I have an API controller that does the following:
// GET api/Exploitation public HttpResponseMessage Get() { var items = _exploitationRepository.FindAll(); var mappedItems = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(items); var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItems); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { })); return response; } // GET api/Exploitation/5 [HttpGet, ActionName("Get")] public HttpResponseMessage Get(int id) { var item = _exploitationRepository.FindById(id); var mappedItem = Mapper.Map<Exploitation, ExploitationView>(item); var response = Request.CreateResponse<ExploitationView>(HttpStatusCode.OK, mappedItem); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id })); return response; } // GET api/Exploitation/GetBySongwriterId/5 [HttpGet, ActionName("GetBySongwriterId")] public HttpResponseMessage GetBySongwriterId(int id) { var item = _exploitationRepository.Find(e => e.Song.SongWriterSongs.Any(s => s.SongWriterId == id)) .OrderByDescending(e => e.ReleaseDate); var mappedItem = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(item); var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItem); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id })); return response; } // GET api/Exploitation/GetBySongwriterId/5 [HttpGet, ActionName("GetBySongId")] public HttpResponseMessage GetBySongId(int id) { var item = _exploitationRepository.Find(e => e.SongId == id) .OrderByDescending(e => e.ReleaseDate); var mappedItem = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(item); var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItem); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id })); return response; }
In my APIConfig, I defined the following routes:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "ActionApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional }, constraints: new { id = @"\d+" } );
I find that I can access the following actions without problems: / api / exploitation / api / exploitation / getbysongwriterid / 1 / api / exploitation / getbysongid / 1
When I try to access / api / operation / 1, I get this exception
"Multiple actions were found that match the request: System.Net.Http.HttpResponseMessage Get(Int32) on type Songistry.API.ExploitationController System.Net.Http.HttpResponseMessage GetBySongwriterId(Int32)" exception.
Can anyone understand what is wrong with my routes? Or is it wrong with something else?