Reputation: 159
I'm attempting to create my own backend for the first time, but I can't figure out how to have multiple Get requests using the same controller. I get the error "The request matched multiple endpoints." So my question is, how can I specify which method to call within the controller? Is this even possible or do I need to have a new controller for each Get request? The two methods that I would like to access are GetSummoner
and GetMatchHistory
. My controller looks like this so far:
public class SummonerController : ControllerBase
{
static HttpClient client = new HttpClient();
[HttpGet()]
public async Task<ActionResult<SummonerModel>> GetSummoner([FromQuery] string summonerName)
{
SummonerModel summoner = null;
HttpResponseMessage response = await client.GetAsync(Global.Global.BASE_URL + "/lol/summoner/v4/summoners/by-name/" + summonerName + "?api_key=" + Global.Global.API_KEY);
if (response.IsSuccessStatusCode)
{
summoner = await response.Content.ReadAsAsync<SummonerModel>();
}
return summoner;
}
[HttpGet()]
public async Task<ActionResult<MatchModel[]>> GetMatchHistory([FromQuery] string accountID)
{
MatchModel[] matches = null;
HttpResponseMessage response = await client.GetAsync(Global.Global.BASE_URL + "/lol/match/v4/matchlists/by-account/" + accountID + "?api_key=" + Global.Global.API_KEY);
if (response.IsSuccessStatusCode)
{
matches = await response.Content.ReadAsAsync<MatchModel[]>();
}
return matches;
}
}
EDIT: I was able to solve this by simply adding the following before the methods
[HttpGet]
[Route("GetSummoner")]
And
[HttpGet]
[Route("GetMatchHistory")]
Thanks to everyone that helped to answer!
Upvotes: 1
Views: 2888
Reputation: 11
All u need to do is to set the correct route.
[HttpGet("summoner")]
[HttpGet("matchhistory")]
Also make sure [ApiController]
is correct.
Upvotes: 1
Reputation: 588
Documentation - You can find your answer here.
So you have two actions with the same route. The way to solve this is to add a new route attribute or change your HtppGet()
attribute because it is overriding the path. So the thing that should work is to remove ()
from your HttpGet
attributes because it is saying that both of these actions are available on URL: .../Summoner/
After removing ()
you should be able to access your actions on paths you want because it will take the name of the action and URL with look like that: .../Summoner/GetSummoner
Upvotes: 1
Reputation: 1
You need to define routes for methods like this.
[RoutePrefix("api/test")]
public class TestController : ApiController
{
[HttpGet]
[Route("get1")]
public IHttpActionResult Get()
{
...
}
}
You can define it in global file also
Upvotes: 2