1

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 }
8
  • Could you show us your WebApiConfig if you made changes there? Commented Oct 13, 2015 at 11:16
  • I don't think using 'Get' in your route names is going to work, as its a special term that is already being translated. Try [Route("RetriveValue")] Commented Oct 13, 2015 at 11:17
  • 1
    Its seems like your web api routing is not correctly configured. With the default routing all get requests will be mapped to the Get method, Posts to Post method etc. Most likely your WebApiConfig.cs has a line containing routeTemplate: "api/{controller}/{id}", which you should change to routeTemplate: "api/{controller}/{action}/{id}" to also map the action. Your [Route] attributes wont be needed for this. Commented Oct 13, 2015 at 11:18
  • config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); Commented Oct 13, 2015 at 11:22
  • see my message above Commented Oct 13, 2015 at 11:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Use This : [ActionName("Getvalue1")]

hope this works!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.