user97492
user97492

Reputation: 93

WHen I create a Blazor WASM project

Can I ask what is the purpose of the .Server project when you use VS 2022 and create a Blazor WASM app. It creates a .Client .Server and .Shared project but when I run the client it doesnt use the .Server at all.

Am I to add all my .razor pages in the .Client or the .Server project?

EDIT: Should I just create everything in the .Client and decide later if I should run the website as a server or wasm?

Upvotes: 2

Views: 416

Answers (3)

JG222
JG222

Reputation: 198

If you are planning to create a web application that does not need to reference any external sources or data (ie. a database, or any other APIs) then you probably don't need the Server project, and probably not the Shared project either for that matter. Basically the Client project is the project that lives and runs in the Client or Browser, your Server project is your backend and is where you should access and run all of your CRUD operations against your database, and the Shared project is a class library that provides class definitions that are shared across both the Client and Server projects. That way you don't need to define your classes and data structures twice, you just do it once and both projects already have a reference to them by default (this is done in the project template, normally you would need to add the project references yourself). The project template sets up the fetch-data page to load randomly generated data to show the weather, and they do this in the Shared project just for simplicity's sake, but typically you would use an HttpClient to make requests to the controllers in the Server project to return data like this. If this is confusing, I would take enet's advice and learn MVC first. Hopefully this clarified things a little.

Upvotes: 1

enet
enet

Reputation: 45764

When you create a Blazor WebAssembly App hosted, the default template contains three projects. The client project contains the (page or routable) components, other objects and components. The client project is for the front-end, and it runs on the browser.

The Server project runs on the server, it is the back-end, and it contains Web Api end points from which data is served to the Fetch Data page. The Server project is actually the starter project and its main purpose is to serve the web app.

The Shared project contains the WeatherForecast object that is shared in both Client and Server projects

Let's analyze this code:

forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");  
  1. forecasts is an array of WeatherForecast objects. Recall that WeatherForecast is defined in the Shared project

  2. await Http.GetFromJsonAsync perform an AJAX call to a Web Api end -point exposed in the Server project:

    public class WeatherForecastController : ControllerBase { }

  3. <WeatherForecast[]>

This tells Microsoft Corporation that we interested to get a list of WeatherForecast objects

  1. ("WeatherForecast"); This is the name of the Controller object (NOT THE NAME OF THE SHARED DATA OBJECT AS YOU THOUGHT). This a actually the url to the end point, and it by deault call the only public Get method defined:

    [HttpGet] public IEnumerable Get() {

    }

More on demands...

Upvotes: 3

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30450

The Client project is where the actual WASM code is compiled.

The server project is an AspNetCore web server project (no Blazor involved) that acts as the host for the WASM project and the API controller site. It's configured to map the wwwroot files in the client project to it's web root.

The Startup project for the solution is the Server project.

Upvotes: 1

Related Questions