C# 从asp.net中的API获取数据

C# 从asp.net中的API获取数据,c#,asp.net,api,C#,Asp.net,Api,我是asp.net新手,尝试从moviedb.org的api获取数据,并在react应用程序中显示数据。 我的模型课在这里准备好了 namespace MovieApi.Models { public class Movie{ public double popularity { get; set; } public int id { get; set; } public bool video { get; set; } p

我是asp.net新手,尝试从moviedb.org的api获取数据,并在react应用程序中显示数据。 我的模型课在这里准备好了

namespace MovieApi.Models
{
    public class Movie{
        public double popularity { get; set; }
        public int id { get; set; }
        public bool video { get; set; }
        public int vote_count { get; set; }
        public double vote_average { get; set; }
        public string title { get; set; }
        public string release_date { get; set; }
        public string original_language { get; set; }
        public string original_title { get; set; }
        public string backdrop_path { get; set; }
        public bool adult { get; set; }
        public string overview { get; set; }
        public string poster_path { get; set; }
    }
}       
下面是我尝试从该API获取数据的过程,但由于某些原因

httpClientFactory.CreateClient("API Client");
抛出错误

Controllers/valuescocontroller.cs(20,26):错误CS0103:名称“httpClientFactory”在当前上下文中不存在[/home/backspace/Downloads/interview coding challenge master/serverside/serverside.csproj]

使用系统;
使用System.Net.Http.Formatting;
使用System.Net.Http;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Newtonsoft.Json;
使用MovieApi.Models;
命名空间值。控制器{
公务舱{
私有异步任务GetMovies()
{
//从我们注册的factpry获取HttpClient实例
//在Startup.cs中
var client=httpClientFactory.CreateClient(“API客户端”);
//调用API并等待响应。
//如果API调用失败,请根据重试策略再次调用它
//在Startup.cs中指定
var result=await client.GetAsync(“/api.themoviedb.org/3/discover/movie?api_key=6&language=en-US&sort_-by=popularity.desc&include_成人=false&include_视频=false&page=1”);
if(结果。IsSuccessStatusCode)
{
//读取所有响应并将其反序列化为
//电影课
var content=wait result.content.ReadAsStringAsync();
返回JsonConvert.DeserializeObject(内容);
}
返回null;
}
}
}

您可能希望使用用于RestSharp的NuGet包,它提供了一个简单的客户端,用于访问RESTful API并将结果反序列化为对象

有关RestSharp的信息可在以下位置找到:

一些伪代码:
_client=新的RestClient(BaseUrl);
var-request=BuildRequest();
var movie=_client.Execute(请求);

验证是否可以在Startup.cs中注入“httpClientFactory”对象。 如果您是可注入的,那么在类中创建一个构造函数来使用his的实例

public class Vals{

    private IHttpClientFactory httpClientFactory;

    public Vals(IHttpClientFactory httpClientFactory)
    {
        this.httpClientFactory = httpClientFactory
    }


    private async Task<Movie> GetMovies()
    {
        // Get an instance of HttpClient from the factpry that we registered
        // in Startup.cs
        var client = httpClientFactory.CreateClient("API Client");

        // Call the API & wait for response. 
        // If the API call fails, call it again according to the re-try policy
        // specified in Startup.cs
        var result = await client.GetAsync("/api.themoviedb.org/3/discover/movie?api_key=6&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1");

        if (result.IsSuccessStatusCode)
        {
            // Read all of the response and deserialise it into an instace of
            // Movie class
            var content = await result.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<Movie>(content);
        }
        return null;
    }
}
公共类VAL{
私人IHttpClientFactory httpClientFactory;
公共VAL(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory=httpClientFactory
}
私有异步任务GetMovies()
{
//从我们注册的factpry获取HttpClient实例
//在Startup.cs中
var client=httpClientFactory.CreateClient(“API客户端”);
//调用API并等待响应。
//如果API调用失败,请根据重试策略再次调用它
//在Startup.cs中指定
var result=await client.GetAsync(“/api.themoviedb.org/3/discover/movie?api_key=6&language=en-US&sort_-by=popularity.desc&include_成人=false&include_视频=false&page=1”);
if(结果。IsSuccessStatusCode)
{
//读取所有响应并将其反序列化为
//电影课
var content=wait result.content.ReadAsStringAsync();
返回JsonConvert.DeserializeObject(内容);
}
返回null;
}
}

将Microsoft提供的有关如何实现HttpClientFactory的文档可视化,我相信它将帮助您:

您是否将其注册为可注射的?我是错过了什么还是你没有注射,你展示了你的api密钥,这对你来说可能是一个安全问题,现在我甚至不知道什么是注入isI,我是这样一个努比,很抱歉带你回去,但有一些资源,我可以用来了解asp.net核心api发生了什么我觉得我在摸索这件事,我真的不知道你能为我提供什么帮助非常感谢。我应该创建一个react应用程序,连接到asp.net API,从另一个API获取数据。老实说,这一切都是在搞乱我的头脑
 Some pseudo-code:
 _client = new RestClient(BaseUrl); 
 var request = BuildRequest();
 var movie = _client.Execute<Movie>(request);
public class Vals{

    private IHttpClientFactory httpClientFactory;

    public Vals(IHttpClientFactory httpClientFactory)
    {
        this.httpClientFactory = httpClientFactory
    }


    private async Task<Movie> GetMovies()
    {
        // Get an instance of HttpClient from the factpry that we registered
        // in Startup.cs
        var client = httpClientFactory.CreateClient("API Client");

        // Call the API & wait for response. 
        // If the API call fails, call it again according to the re-try policy
        // specified in Startup.cs
        var result = await client.GetAsync("/api.themoviedb.org/3/discover/movie?api_key=6&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1");

        if (result.IsSuccessStatusCode)
        {
            // Read all of the response and deserialise it into an instace of
            // Movie class
            var content = await result.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<Movie>(content);
        }
        return null;
    }
}