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 }
[Route("RetriveValue")]routeTemplate: "api/{controller}/{id}", which you should change torouteTemplate: "api/{controller}/{action}/{id}"to also map the action. Your[Route]attributes wont be needed for this.