Hi @Ronald Rex ,
To access the API controller in the Blazor Server app, you can use the HttpClient. You can refer the following samples:
- Create a API application with the following code:
Then, run the application, the query result as below: request url is :[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } [HttpGet] public IEnumerable<WeatherForecast> Get() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray(); } }
https://localhost:7114/WeatherForecast
- In the Blazor Server app
Then in the FetchData.razor component, inject the HttpClient and call the API methods://if the API controller is in the same project, //you can use `new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }` to set the base address. builder.Services.AddScoped(sp => new HttpClient());
View the source code: 225167-sourcecode.txt After running the Blazor server application, the result as below:
More detail information about using HttpClient, refer to the following links:
Make HTTP requests using IHttpClientFactory in ASP.NET Core
Call a web API from ASP.NET Core Blazor
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion