Reputation: 33
I am struggling to make Route Prefix working or probably have misunderstanding about Routes in ASP.NET Web API
Here is my code:
public class TestController : ApiController
{
// GET: api/Test
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/Test/5
public string Get(int id)
{
return "value";
}
[HttpGet]
[Route("GetValue")]
public string GetValue(int id)
{
return "best";
}
[HttpGet]
[Route("GetValue1")]
public string GetValue1(int test)
{
return "GetValue";
}
}
Here is the status of each call
localhost:52154/api/test/Getvalue1/1
(HTTP Error 404.0 - Not Found)localhost:52154/api/test/Getvalue1?test=1
(
The webpage cannot be found )localhost:52154/api/test/Getvalue?id=1
(Works return "value" but does not call the GetValue Method)localhost:52154/api/test/1
(Works return "value")localhost:52154/api/test/
(Works return "value1","value2")Please let me know what to do to make the GetValue1 and GetValue route work. I did update the NuGet pacakge for ASP.NET to Web Api 2.2
WebApiConfig looks like this
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
Upvotes: 1
Views: 73
Reputation: 33
Here is the solution which worked for me public IEnumerable Get() { return new string[] { "value1", "value2" }; }
// GET: api/Test/5
public string Get(int id)
{
return "value";
}
[HttpGet]
public string GetValue(int id)
{
return "best";
}
[HttpGet]
public string RetriveValue(int test)
{
return "GetValue";
}
courtesy of How to bypass the exception multiple actions were found in ASP.NET Web API
Upvotes: 1