我正在尝试简单地显示 github 存储库。网址“https://api.github.com/search/repositories?q=pluralsight https://api.github.com/search/repositories?q=pluralsight“在我的浏览器中工作返回 json 并在 fiddler 中工作,但我的 .NET Web 应用程序中的以下内容收到 403 Forbidden 错误。任何人都可以帮助我理解修复吗?我的控制器如下:
public class HomeController : Controller
{
public ActionResult Index()
{
Tweets model = null;
var client = new HttpClient();
var task = client.GetAsync("https://api.github.com/search/repositories?q=pluralsight")
.ContinueWith((taskwithresponse) =>
{
var response = taskwithresponse.Result;
response.EnsureSuccessStatusCode();
var readtask = response.Content.ReadAsAsync<Tweets>();
readtask.Wait();
model = readtask.Result;
});
task.Wait();
return View(model.results);
}
}
我有一个定义如下的类(忽略它称为 Tweets)最初尝试访问 twitter api。
namespace HttpClientMVCDemo.Controllers
{
public class Tweets
{
public Tweet[] results;
}
public class Tweet
{
[JsonProperty("name")]
public string UserName { get; set; }
[JsonProperty("id")]
public string id { get; set; }
}
}
基于 Amit 的以下类自动生成的代码视图:
@model IEnumerable<HttpClientMVCDemo.Controllers.Gits>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.total_count)
</th>
<th>
@Html.DisplayNameFor(model => model.incomplete_results)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.total_count)
</td>
<td>
@Html.DisplayFor(modelItem => item.incomplete_results)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /*id=item.PrimaryKey*/ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}