3
votes

Using Template: Blazor WebAssembly App with ASP.NET Core hosted enabled

It creates projects:

  • BlazorApp1.Client
  • BlazorApp1.Server

I can see that server has a project reference to the Client, but I cannot determine what code allows the server to display the client?

How does the server know that the client index file is in another assembly?

Is the client code being hosted in the server project on the same port?

I'm a bit lost trying to figure this out.

1
It's sets a bad example that the server project has a reference to the client. Disappointed in Microsoft, but not surprised, they've never been able to do web right.Paul McCarthy

1 Answers

3
votes

The project reference is just there so that MsBuild (VS) can copy the client files to the server's bin directory.

The server then has a special middleware component to serve the client. The following lines are both about serving static files:

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

The Server does not actually link to or reference any code in the Client.

Is the client code being hosted in the server project on the same port?

Yes, just as a convenience. You could host the client somewhere else (but then also think about CORS settings).

I cannot determine what code allows the server to display the client?

That happens with the last line in UseEndPoints :

endpoints.MapControllers();                  // handle /api
endpoints.MapFallbackToFile("index.html");   // everything else

Do note that because of this your API will never return a 404 code. It will return the "nothing here" page of your client app instead. And you will see a "HTML isn't JSON" related error.