如何使用restapi向Github存储库发出api请求?

克里斯莉27

我是C#和.Net的新手,我正努力了解我遇到的这个问题。我觉得我所拥有的已经足够,但不确定自己。所以,如果有人可以只看一下我下面的代码并告诉我要纠正的话,什么是错的。谢谢

这是我的模特班

namespace github_project.Models
{
    public class GithubItem
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string FullName { get; set; }
        public string City { get; set; }
        public string ProjectName { get; set; }
        public string Commits { get; set; }
        public double Rating { get; set; }
        public string AvatarUrl { get; set; }
    }
}

这是我的数据库上下文

namespace github_project.Database
{
    public class GithubContext : DbContext
    {
        public DbSet<GithubItem> Github { get; set; }
        public GithubContext(DbContextOptions<GithubContext> options) : base(options)
        {
        }

        public GithubItem ItemsList()
        {
            List<GithubItem> build = Build();
            GithubItem itemsList = JsonConvert.DeserializeObject<GithubItem>(build);
            return itemsList;
        }

        public List<GithubItem> Build()
        {
            var getData = GetGithubData();
            return System.Text.Json.JsonSerializer.Deserialize<List<GithubItem>>(getData);
        }

        private string GetGithubData()
        {
            string username = "**test**";
            var url = "https://api.github.com/users/" + username + "/repos?page=1";

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json";
            request.UserAgent = "TestApp";

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                return reader.ReadToEnd();
            }
        }

        public List<GithubItem> getGithub() => Github.Local.ToList<GithubItem>();
    }
}

终于这是我的控制器

[HttpGet("/github")]
public GithubItem GetAll()
{
    return _context.ItemsList();
}

我正在向github请求,以获取所有数据并在我的请求中使用它。我在这里通过以下方法将Collection.List转换为String:

public GithubItem ItemsList()
{
    List<GithubItem> build = Build();
    GithubItem itemsList = JsonConvert.DeserializeObject<GithubItem>(**build**);
    return itemsList;
}

有人可以帮我,有人可以告诉我这是怎么回事吗???谢谢

小威利·戴维(Willy David Jr)

您不能反序列化对象或将对象从转换List<GithubItem>为single GithubItem那就是你在做什么。

如您所见,您具有build

List<GithubItem> build = Build();

build变量是一个List<GithubItem现在,您想使用JsonConvert的反序列化将其转换为单个吗?

无论您的要求是什么,您都可以使用以下代码获得一条记录:

 GithubItem itemsList = build.FirstOrDefault();

那会很好的。但这只是一个例子,因为我不确定您的要求是什么。如果需要过滤记录,还可以在FirstOrDefault上传递参数:

 GithubItem itemsList = build.FirstOrDefault(x => x.UserName == "John");

那也可以。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用python向RESTful API发出请求

使用go向mojang API发出请求会得到403

如何优化向API发出请求的for循环?

如何连续向API发出GET请求?

使用Laravel向一个API发出多个请求

如何使用Scribe通过POST向自定义API发出OAuth2授权请求

向Google Contacts REST API发出HTTP GET请求时如何使用API密钥

如何使用TcpClient向网站发出请求

在发出拉取请求后,如何仅将其推入我的Github存储库的分支中

如何向zomato api发出cURL请求?

如何在Github上对用户的所有存储库发出一个异步请求

我如何向API发出请求

如何向Amadeus API发出请求?网络错误

如何向数据库发出多个请求?

如何使用Angular向API网关发出Get请求?

如何在不推送远程分支的情况下使用新的github cli向远程仓库发出请求请求?

Python-如何使用请求向API发出PATCH请求

如何从getStaticProps向API路由发出请求

InvalidURIError使用Ruby向Facebook Graph API发出请求

如何使用GitHub API获取GitHub存储库的派生数?

如何使用临时凭证从 Postman 向 AWS API Gateway 发出请求

使用python向Stackoverflow API发出请求时,如何通过标头传递我的API密钥

如何使用 GitHub API 搜索存储库的分叉存储库?

如何使用 ASP.NET Core 2.2 向 am API 发出 PATCH 请求?

如何通过在 Firefox 中 fetch js 向 API 发出请求?

如何使用 SAS 密钥向 Azure 表存储发出 Invoke-RestMethod GET 和 PUT 请求

如何使用 offset 和 limit 参数向 musicBrainz api 发出浏览请求

如何使用 AuthorizedAttribute 向 API 控制器发出 HTTP 请求?

如何使用 github API 创建从分叉存储库返回到原始存储库的 github 拉取请求?