I am using asp.net mvc5 and webapi 2. Here is my sample controller -
public class DataFetchController : ApiController { public async Task<HttpResponseMessage> GetDO() { return Request.CreateResponse("ok"); } public async Task<HttpResponseMessage> GetDoes() { return Request.CreateResponse("ok"); } } In my WebApiConfig.cs file I have the following code -
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); When I run this project and try to browse "api/DataFetch/getdo" link then i encountered an error that shows -
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nGetDO on type MagicMeter.Controllers.DataFetchController\r\nGetDoes on type MagicMeter.Controllers.DataFetchController","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}
How can I fix this problem??
actionis determined by the http verb, in this caseGET. As there's only one verb (GET), there's only one action which is determined automatically (or by tags) by the name "Get*". You can have multiple actions by having different overloads (egGetAll()andGet(id)) but not if they have the same signature.