Calling REST API from BLAZOR Application

In this article, we will see how to consume REST API calls inside blazor application,

Initially, we are loading the data by hard coding the values in component base file lets look at the code for that,

Productlist.razor

@page “/”

ProductlistBase.cs

using Microsoft.AspNetCore.Components;

So as you see over here we are hard coding the values and displaying it on component now instead we will get the product list from database calling a rest api defined method for that we are defining service to get that data,

IProductService.cs

using System;

ProductService.cs

using Microsoft.AspNetCore.Components;

As you see over here we used HttpClient to make a call to our endpoint for that you need to register this service in ConfigureService as shown below,

public void ConfigureServices(IServiceCollection services)

Here, we registered HttpClient with respective service in Dependency injection container and also specified the base address of the client.

Once these changes are done then we need to inject the service in the component base class to fetch the required data as shown below,

ProductlistBase.cs

using EmployeeManagement.Web.Services;

Thank You, See you in the next article !!

You can reach out to me here,

LinkedIn: https://www.linkedin.com/in/vaibhav-bhapkar

Email: vaibhavbhapkar.medium@gmail.com

If you want some technical topics to be discussed with group of participants please raise a request on following link: http://myblogsenquiry.somee.com/

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *