Call method Restful Api
есть Solutiion в котором находиться два проекта Lemondo.Api и Lemondo.UI
в проекте Lemondo.Api есть котроллер WeatherForecastController
[ApiController]
[Route("api/[controller]/[action]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IStatement _statement;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IStatement statement)
{
_statement = statement;
_logger = logger;
}
[HttpGet]
public async Task<Statement> GetStamenet(int id)
{
return await _statement.GetStatementById(id);
}
}
а втором проекте есть котроллер, с этоно котроллера хочу вызвать метод GetStamenet который находиться WeatherForecastController в этом котроллере
public class CrudController : Controller
{
private readonly HttpClient _client;
public CrudController(IHttpClientFactory factory)
{
_client = factory.CreateClient("api");
}
[HttpGet]
[Route("/crud/getStatement")]
public async Task<IActionResult> GetStatement(int id)
{
var statement = await _client.GetListAsync<Statement>($"api/WeatherForecast/GetStamenet/{id}");
return View();
}
}
public static class HttpClientExtentions
{
public static async Task<List<T>> GetListAsync<T>(this HttpClient _client, string url)
{
var response = await _client.GetAsync(url);
if (!response.IsSuccessStatusCode) throw new Exception("todo make normalize error");
var data = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<T>>(data);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddHttpClient("api", options =>
{
options.BaseAddress = new Uri(Configuration.GetValue<string>("api"));
});
}
но не получается в ConfigureServices выдает ошибку System.ArgumentNullException: 'Value cannot be null. (Parameter 'uriString')'