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
Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.