我有一个名为ApiHandler
的类,它是一个泛型。现在,我在该类中声明了两个实例变量,并希望通过静态方法对其进行访问。
到目前为止,这就是我尝试过的方式
public class ApiHandler<T> where T : class
{
private readonly IConfiguration _configuration;
private readonly IHttpContextAccessor _contextAccessor;
public ApiHandler(IConfiguration configuration, IHttpContextAccessor contextAccessor)
{
_configuration = configuration;
_contextAccessor = contextAccessor;
}
#region Normal
public static async Task<T> GetAsync(string url)
{
using (var client = new HttpClient())
{
// can't access this `_configuration` here
string baseUrl = _configuration.GetSection("BaseUrl").Value;
client.BaseAddress = new Uri("https://localhost:44345/api/");
// Get token from cookies here
var responseTask = await client.GetAsync(url);
if (responseTask.IsSuccessStatusCode)
{
return await responseTask.Content.ReadAsAsync<T>();
}
else
{
throw new Exception("Server error. Please contact administrator.");
}
}
}
}
在这里,我想访问_configuration
方法内部的GetAsync
变量,该方法是静态方法。
我怎样才能做到这一点
请帮帮我!!!