C
C#2mo ago
Acetylene

Blazor WebAssembly API Controller

Hi all, I am pretty new in C#. Now, I'm working on a Blazor web project. My project structure is like that: | MealOrdering (Blazor WebAssembly) ( MealOrdering.Server.Data and MealOrdering.Shared added as dependent projects) | MealOrdering.Server.Data (Class Library) | MealOrdering.Shared (Class Library) I created models in MealOrdering.Server.Data, DTOs in MealOrdering.Shared, services in MealOrdering. Also added AutoMapper package to MealOrdering and created Services/Extensions/ConfigureMappingExtension.cs. Finally, in MealOrdering, I created a folder which is named Controllers and a API Controller which is named UserController.cs. I added a method for handling GET request.
[HttpGet("Users")]
public async Task<ServiceResponse<List<UserDTO>>> GetUsers()
{
return new ServiceResponse<List<UserDTO>>
{
Value = await userService.GetUsers()
};
}
[HttpGet("Users")]
public async Task<ServiceResponse<List<UserDTO>>> GetUsers()
{
return new ServiceResponse<List<UserDTO>>
{
Value = await userService.GetUsers()
};
}
However, when I go to https://localhost:44325/api/User/Users, it returns Not Found... What should I do to get users? Project structure has been attached. I tried my best so hope it's clear to get help.
No description
5 Replies
Salman
Salman2mo ago
You can't put controllers in WASM. WASM is client side, you would need to make a separate project for the API and call that api from WASM for serverr side stuff
Acetylene
AcetyleneOP2mo ago
wow, got it. thank you
Salman
Salman2mo ago
also .Server.Data is a redundant project. Just put them in your Api project
Acetylene
AcetyleneOP2mo ago
thanks, actually I can’t determine what is necessary now. just trying to learn something. I was watching a blazor series which uses .net 5. I think i need to find a newer one.
Salman
Salman2mo ago
Yes the latest version is .Net 9 now

Did you find this page helpful?