Blazer Web Server App using .Net Core 6 API

Ronald Rex 1,666 Reputation points
2022-07-27T06:02:33.623+00:00

Hi Friends I am using a Blazer Web Server App to connect to a .Net Core 6 API. I am having trouble finding a tutorial.. Maybe someone can steer me in the right direction. Thanks !!!!

Developer technologies .NET Blazor
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-07-27T08:06:37.963+00:00

    Hi @Ronald Rex ,

    To access the API controller in the Blazor Server app, you can use the HttpClient. You can refer the following samples:

    1. Create a API application with the following code:
      [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();  
          }  
      }  
      
      Then, run the application, the query result as below: request url is : https://localhost:7114/WeatherForecast 225130-image.png
      1. In the Blazor Server app
      Register the HttpClient service in the Program.cs file:
      //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());   
      
      Then in the FetchData.razor component, inject the HttpClient and call the API methods: 225192-image.png View the source code: 225167-sourcecode.txt After running the Blazor server application, the result as below: 225211-image.png

    More detail information about using HttpClient, refer to the following links:

    Make HTTP requests using IHttpClientFactory in ASP.NET Core

    HttpClient Class

    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


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.