CoolArsh
CoolArsh

Reputation: 33

ASP.NET Routing not working

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

  1. localhost:52154/api/test/Getvalue1/1 (HTTP Error 404.0 - Not Found)
  2. localhost:52154/api/test/Getvalue1?test=1 ( The webpage cannot be found )
  3. localhost:52154/api/test/Getvalue?id=1 (Works return "value" but does not call the GetValue Method)
  4. localhost:52154/api/test/1 (Works return "value")
  5. 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

Answers (2)

CoolArsh
CoolArsh

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

Varun Vasishtha
Varun Vasishtha

Reputation: 461

Use This : [ActionName("Getvalue1")]

hope this works!!

Upvotes: 0

Related Questions