I used the Auto render mode template, so VS 2022 created 2 projects for me. One is xxxProjectName, the other one is xxxProjectName.Client.
xxxProjectName is the project to be hosted in server, and the xxxProjectName.Client is the project to be downloaded to browser, am I right?
I want to put all logic at server side, so I expose a controller for Web API, and add HttpClient to xxxProjectName.Client to call the Web API.
After I added HttpClient service in Program.cs in xxxProjectName.Client, and try to inject it in Razor Page, it failed to find the HttpClient, only works if I add HttpClient service in Program.cs in xxxProjectName. I am confused, I set the @rendermode InteractiveAuto in the Razer Page in the xxxProjectName.Client, but why do I need to register the service in server side? If so, why do we need a xxxProjectName.Client project?
Another thing is why both xxxProjectName and xxxProjectName.Client have a Layout folder, and put MainLayout and NavMenu in different part?
I am confused, I set the @rendermode InteractiveAuto in the Razer Page in the xxxProjectName.Client, but why do I need to register the service in server side?
You need to register in both Program.cs files.
Auto rendermode means that the page is first rendered server-side, that is why you got the error. When you re-visit the page it will run on webassembly and then you will need the HttpClient you registered in the Client project.
In this setup I would elect InteractiveWebAssembly instead of Auto, just to avoid this.