Reputation: 229
I trying to achieve something like this
namespace CoreAPI.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpGet]
public string GetValue(string name,string surname)
{
return "Hello " + name;
}
}
}
I want to call this controller method by using both of these URLs:
Upvotes: 5
Views: 10578
Reputation: 16811
You can solve this by defining multiple routes on top of the controller method
[HttpGet("GetValues")]
[HttpGet("GetValues/{name}/{surname}")]
public string GetValue(string name, string surname)
{
return "Hi" + name;
}
This will work with http://localhost:11979/api/values/GetValues/John/lawrance
and http://localhost:11979/api/values/GetValues?name=john&surname=lawrance
To add more:
[HttpGet]
[Route("GetValues")]
[Route("GetValues/{name}/{surname}")]
public string GetValue(string name,string surname)
{
return "Hello " + name + " " + surname;
}
This also works.
Upvotes: 9