如何在 ASP.NET MVC 应用程序上使用 Github API?
How to use Github API on a ASP.NET MVC app?
我是 ASP.NET MVC 领域的新手,我一直在努力更加熟悉 .NET 平台及其特性。目前,我正在尝试制作一个 C# MVC 应用程序,它使用 Github API 来检索一些数据,例如列表我所有的存储库,显示他们的一些信息,按名称(不一定是我的)搜索存储库等。
但我在简单的事情上苦苦挣扎。首先,我不知道如何建立正确的连接来获取这些信息。我尝试按照他们页面上的 octokit 教程进行操作,并尝试使用 JSON 检索数据并转换为 C# 对象,但我的所有尝试都失败了。
我在 octokit 中看到了这个例子,它适用于控制台应用程序:
var github = new GitHubClient(new ProductHeaderValue("MyAmazingApp"));
var user = await github.User.Get("half-ogre");
Console.WriteLine(user.Followers + " folks love the half ogre!");
但考虑到 ASP.NET 上的 MVC 结构,或者它应该有何不同,我很困惑应该把它们放在哪里。我没有发现 octokit 文档在这些方面很有帮助。
到目前为止,我已经完成了一些尝试将 JSON 反序列化为 C# 的操作:
型号
namespace GithubAPI.Models {
public class Repositories {
public string name { get; set; }
public string language { get; set; }
public string owner { get; set; } //or login ?
public string updated_at { get; set; }
}
public class RepCollections {
private List<Repositories> repositories;
public List<Repositories> Repositories { get => this.repositories; set => this.repositories = value; }
}
}
我在尝试将 JSON 数据保存到 Class
时这样做了
控制器
public ActionResult Index() {
var url = "https://api.github.com/users/{myuser}/repos";
using (var webClient = new WebClient()) {
var rawJSON = string.Empty;
try {
rawJSON = webClient.DownloadString(url);
}
catch (Exception) { }
RepCollections rep = JsonConvert.DeserializeObject<RepCollections>(rawJSON);
return View();
}
}
没用,可能是放错地方了,idk。
查看
@using GithubAPI.Models
@model RepCollections
@{
ViewBag.Title = "Repositories";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
<h2>Repositories</h2>
<br/>
<p>User: @Html.DisplayNameFor(model => model.owner)</p>
<div>
<table class="table">
<tbody>
<tr><th></th></tr>
@foreach (Repositories r in Model) {
<tr>
<td>
@r.name
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
不用说 foreach
没用。
有人可以给我一些启发吗?谢谢。
P.S.: 我没有使用 OAuth。只想要一个基本的身份验证。
您的 foreach 循环不工作,因为您没有将模型从 Controller 传递到 View。试试这个:
控制器
public ActionResult Index()
{
var url = "https://api.github.com/users/{myuser}/repos";
using (var webClient = new WebClient())
{
var rawJSON = webClient.DownloadString(url);
RepCollections rep = JsonConvert.DeserializeObject<RepCollections>(rawJSON);
return View(rep);
}
}
查看
@using GithubAPI.Models
@model RepCollections
@{
ViewBag.Title = "Repositories";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
<h2>Repositories</h2>
<br />
<p>User: @Html.DisplayNameFor(model => model.owner)</p>
<div>
<table class="table">
<tbody>
<tr><th></th></tr>
@foreach (Repositories r in Model)
{
<tr>
<td>
@r.name
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
我是 ASP.NET MVC 领域的新手,我一直在努力更加熟悉 .NET 平台及其特性。目前,我正在尝试制作一个 C# MVC 应用程序,它使用 Github API 来检索一些数据,例如列表我所有的存储库,显示他们的一些信息,按名称(不一定是我的)搜索存储库等。 但我在简单的事情上苦苦挣扎。首先,我不知道如何建立正确的连接来获取这些信息。我尝试按照他们页面上的 octokit 教程进行操作,并尝试使用 JSON 检索数据并转换为 C# 对象,但我的所有尝试都失败了。
我在 octokit 中看到了这个例子,它适用于控制台应用程序:
var github = new GitHubClient(new ProductHeaderValue("MyAmazingApp"));
var user = await github.User.Get("half-ogre");
Console.WriteLine(user.Followers + " folks love the half ogre!");
但考虑到 ASP.NET 上的 MVC 结构,或者它应该有何不同,我很困惑应该把它们放在哪里。我没有发现 octokit 文档在这些方面很有帮助。
到目前为止,我已经完成了一些尝试将 JSON 反序列化为 C# 的操作:
型号
namespace GithubAPI.Models {
public class Repositories {
public string name { get; set; }
public string language { get; set; }
public string owner { get; set; } //or login ?
public string updated_at { get; set; }
}
public class RepCollections {
private List<Repositories> repositories;
public List<Repositories> Repositories { get => this.repositories; set => this.repositories = value; }
}
}
我在尝试将 JSON 数据保存到 Class
时这样做了控制器
public ActionResult Index() {
var url = "https://api.github.com/users/{myuser}/repos";
using (var webClient = new WebClient()) {
var rawJSON = string.Empty;
try {
rawJSON = webClient.DownloadString(url);
}
catch (Exception) { }
RepCollections rep = JsonConvert.DeserializeObject<RepCollections>(rawJSON);
return View();
}
}
没用,可能是放错地方了,idk。
查看
@using GithubAPI.Models
@model RepCollections
@{
ViewBag.Title = "Repositories";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
<h2>Repositories</h2>
<br/>
<p>User: @Html.DisplayNameFor(model => model.owner)</p>
<div>
<table class="table">
<tbody>
<tr><th></th></tr>
@foreach (Repositories r in Model) {
<tr>
<td>
@r.name
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
不用说 foreach
没用。
有人可以给我一些启发吗?谢谢。
P.S.: 我没有使用 OAuth。只想要一个基本的身份验证。
您的 foreach 循环不工作,因为您没有将模型从 Controller 传递到 View。试试这个:
控制器
public ActionResult Index()
{
var url = "https://api.github.com/users/{myuser}/repos";
using (var webClient = new WebClient())
{
var rawJSON = webClient.DownloadString(url);
RepCollections rep = JsonConvert.DeserializeObject<RepCollections>(rawJSON);
return View(rep);
}
}
查看
@using GithubAPI.Models
@model RepCollections
@{
ViewBag.Title = "Repositories";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
<h2>Repositories</h2>
<br />
<p>User: @Html.DisplayNameFor(model => model.owner)</p>
<div>
<table class="table">
<tbody>
<tr><th></th></tr>
@foreach (Repositories r in Model)
{
<tr>
<td>
@r.name
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>