skip to Main Content

I’m still at the beginning level, and my question may be a little naive.

Both HttpClient and directly calling class methods can request data from the server, so why use HttpClient instead of class methods?

e.g.

backend:

public Class DataService
{
    private readonly DataContext _db;
    
    public DataService(DataContext db)
    {
        _db = db;
    }
    
    public async Task<List<Datas>> GetDatasAsync()
    {
        return await _db.Datas.ToListAsync();
    }
    
    [HttpGet]
    public async Task<ActionResult<List<Datas>>> GetDatasHttpAsync()
    {
        return await _db.Datas.ToListAsync();
    }
}

fontend:

HttpClient:
httpDatas = await HttpClient.GetFromJsonAsyncList<Datas>(NavigationManager.BaseUri + "datas");

Class Method:
[inject DataService ds]
datas = await ds.GetDatasAsync();

In the front-end code block, both methods can get back data, why use the httpclien method instead of the class method

I searched for a long time, did not find the answer, ask seniors for advice

2

Answers


  1. Backend:

    This is the part of the web application that runs on the server and is responsible for data processing, business logic, interaction with the database and other server operations.
    The commonly used programming languages ​​and frameworks.

    Frontend:

    Usually it is the part of a web application that users interact with directly in their browser or client device.
    The frontend is responsible for displaying content, interactive elements, user interface and user experience.
    It uses markup languages ​​(HTML), styling (CSS), and programming languages ​​(JavaScript) to create web pages with interactivity.

    Both components – backend and frontend – interact with each other via the network, exchanging data and requests. For example, when a user requests a page from a web application, the request is first processed on the backend, which then sends data to the frontend to be displayed to the user.

    For different remote systems, a type of remote data exchange is used via web protocols(ex HTTP) using clients servicing(HttpClient) these protocols.

    Login or Signup to reply.
  2. I need to check that I understand your question correctly. I think you are asking, "With Blazor Server, why use a HttpClient and a Web API to get data from the backend, when I can just interact with the database directly from the frontend?"

    It’s quite ok to use either method with Blazor Server. The reason that it might be useful to use HttpClient to call a backend API is so that you could switch to Blazor WebAssembly (WASM) in the future very easily.

    Blazor Server runs on the server, and parts of the UI are synchronised between the client and the server. You can picture it more like an MVC application with a JS managing the dynamic interactions. Therefore it’s safe to make database calls from a page or component, because this code lives on the server.

    Blazor WebAssembly runs in the client’s browser, and therefore they could not make direct calls to your database like Blazor Server can, because the code is executed from the client’s browser. It should not be able to reach your database.

    So by splitting database work into backend services and then calling them via a HttpClient, it makes that part of the application compatible with Blazor Server Blazor WebAssembly.

    You can read more about Blazor’s hosting models here:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/hosting-models?view=aspnetcore-8.0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search