简体   繁体   English

为什么我在制作 asp.net web api 时收到此错误?

[英]Why am I getting this error while making a asp.net web api?

Controller.cs Controller.cs

[HttpGet]
    public async Task<ActionResult<IEnumerable<RankDistribution>>> GetTodoItems()
    {
        List<rank> rklist = new List<rank>();
        rank rk = new rank
        {
            rankID = 0,
            rankName = "Plastic 1",
            playersInRank = 1,
            percentInRank = 100f
        };
        rklist.Add(rk);
        var rb = new RankDistribution
        {
            Id = 0,
            rank = rklist
        };
        return rb;
    }

Class.cs Class.cs

public class RankDistribution
{
    public long Id { get; set; }
    public List<rank> rank { get; set; }
}
public class rank
{
    public string rankName { get; set; }
    public int rankID { get; set; }
    public int playersInRank { get; set; }
    public float percentInRank { get; set; }
}

Error Message错误信息

Cannot implicitly convert type 'RankAPI.Models.RankDistribution' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<RankAPI.Models.RankDistribution>>

I have only recently started working on an api, but I am pretty familiar on C#.我最近才开始研究 api,但我对 C# 非常熟悉。 How do I convert the return value so it goes through?如何转换返回值以使其通过? Thanks!谢谢!

You are returning a single item of RankDistribution您正在返回 RankDistribution 的单个项目

You should include it in a List and return the list您应该将其包含在列表中并返回列表

var toRet = new List<RankDistribution>();
toRet.Add(rb);
return toRet;

But in my opinion if you have the RankDistribution that contains a list inside of the items you want to return why not returning just rb and change the syntax of your method to但在我看来,如果你有 RankDistribution 包含要返回的项目内的列表,为什么不只返回 rb 并将方法的语法更改为

ActionResult<RankDistribution>

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

相关问题 为什么在ASP.NET中的Web配置中出现错误? - Why am I getting an error in web config in ASP.NET? ASP.NET Core Web API &amp; Entity Framework Core; 为什么我收到这个错误? - ASP.NET Core Web API & Entity Framework Core; why am I getting this error? 在asp.net中使用会话时出现错误 - I am getting error while using sessions in asp.net ASP.Net MVC 应用程序在进行 Web API 调用时抛出 403 错误 - ASP.Net MVC application throws 403 error while making a Web API call 当我在MVC中使用ASP.NET Web API时,无法在JSON响应中获取列表数据 - Not Getting the list data in json responce when i am using the asp.net web api in mvc 为什么在上传处理时我的 ASP.NET Core 7 MVC 中出现 FileNotFOund 异常? - Why am I getting a FileNotFOund Exception in my ASP.NET Core 7 MVC while upload handling? 在ASP.NET WEB API调用上发布登录信息时出错 - Getting error while posting login info on ASP.NET WEB API call 为什么我在asp.net网站启动时收到ThreadAbortException - Why am I getting a ThreadAbortException on asp.net website startup 为什么我在 ASP.NET Core 6 C# 中收到此错误? - Why am I getting this error in ASP.NET Core 6 C#? 为什么我无法删除我的数据并收到错误 asp.net mvc webapplication? - Why I am not able to delete my data and getting error asp.net mvc webapplication?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM