When I create an ASP.NET Core hosted Blazor app from the .NET template I get something like this:
I can easily run dotnet publish
on the Web Project and will get an executable that hosts all the content of the Blazor web app.
So if I modify the project structure to include a Desktop UI with separation of concerns I might eventually end up with something like this:
Microsoft.NET.Sdk
with UseWPF
set to true
OutputType
is set to WinExe
Microsoft.NET.Sdk.Web
to have access to the ASP.NET Core runtimeOutputType
is set to Library
Microsoft.NET.Sdk.BlazorWebAssembly
OutputType
is set to Exe
(default) If I now run dotnet publish
on the UI Project , I will end up with an executable that shows a user interface and has ASP.NET Core hosting stuff but accessing the Blazor web app will fail, since the wwwroot
folder will not be published that way.
Now there are two ways to fix this issue that I am aware of. Either you make the UI Project also use the Microsoft.NET.Sdk.Web
or you explicitly publish the Web Project into the same folder as the UI Project .
Both workarounds seem wrong to me, either you have to know to explicitly publish a project that is not a top level project or you need to modify your top level project to make all aspects of your referenced projects work.
That gets me to the question, what is the right way to correctly publish (web) applications in .NET?
edit : Edited the title and the modified project structure to hopefully make things more clear.
You probably need to structure the projects within your solution with the correct dependancies.
The diagrams below are lifted from one of my demo solutions to show one possible project structure. The arrows represent inter project dependancies. This design is based on Clean Design principals. I change the startup project to run in the different modes.
In this design your "Desktop App" or "Mobile App" will have a UI project (Blazr.Demo.DesktopUI), whatever other projects are needed to support the Desktop application, and a top level build project Blazr.Demo.Desktop...
This shows the Blazor Server project dependancies:
And this the WASM project dependancies.
My solution consists of nine projects, which may seem overkill, but organises the code logically. The projects enforce the design dependencies and separation of concern principles.
BlazorWebAssembly
project. It builds the WASM code to deploy to the browser.AspNetCore
Web project, configured to support the Blazor Server Hub, and contains the launch page for the Blazor Server SPA. AspNetCore
Web project configured to provide server side files for the WASM SPA, the launch page for the Blazor Server SPA, and any API controllers. If this answer doesn't help, no problem, I'll delete it!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.