简体   繁体   English

一个 controller 中的多个 Get 请求

[英]Multiple Get requests in one controller

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.我第一次尝试创建自己的后端,但我不知道如何使用相同的 controller 来处理多个 Get 请求。 I get the error "The request matched multiple endpoints."我收到错误“请求匹配多个端点”。 So my question is, how can I specify which method to call within the controller?所以我的问题是,如何指定在 controller 中调用哪个方法? Is this even possible or do I need to have a new controller for each Get request?这是否可能,或者我是否需要为每个 Get 请求提供一个新的 controller? The two methods that I would like to access are GetSummoner and GetMatchHistory .我想访问的两种方法是GetSummonerGetMatchHistory My controller looks like this so far:到目前为止,我的 controller 看起来像这样:

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!感谢所有帮助回答的人!

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您也可以在全局文件中定义它

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.解决这个问题的方法是添加一个新的路由属性或更改您的HtppGet()属性,因为它覆盖了路径。 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所以应该工作的事情是从您的HttpGet属性中删除()因为它说这两个操作都可以在 URL 上使用:.../Summoner/ 删除()之后,您应该能够访问您在路径上的操作想要,因为它将采用动作名称和 URL 看起来像这样: .../Summoner/GetSummoner

All u need to do is to set the correct route.您需要做的就是设置正确的路线。

[HttpGet("summoner")]

[HttpGet("matchhistory")]

Also make sure [ApiController] is correct.还要确保[ApiController]是正确的。

Don't know if that's the problem, but it has to be [HttpGet] (no ())不知道是不是这个问题,但必须是[HttpGet](没有())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM