Reputation: 192
I'm new to .net, C# and currently write my first Blazor(servcer-side) app. Later i want to migrate to Blazor webassembly..
However, I came to the question, do I need controllers?
I know Controllers in the form of NestJS - that Controllers handle http requests and use Services for data exchange and other tasks.
But because currently everything is handled server-side no http requests are made.. So I can't think of a case, where this (division) could come handy..
And furthermore, are Controllers used in WebAssembly Blazor apps, where actual http requests are made?
Because here I can imagine that such a division is advantageous.
Thanks in advance.
Upvotes: 8
Views: 8239
Reputation: 119
I think it's better if you query your data via api controllers even for Blazor Server (they could be implemented inside Blazor Server project) so that it'd be much easier to migrate from server to wasm
I just created a Blazor server app and I injected the DAL into each component (instead of using api controllers) cause it was much faster to develop but when I tried to migrate to wasm it was very hard because I'll have to rewrite most of it
Upvotes: 1
Reputation: 8671
Every time you browse to a website, you are making an http request. Asp.Net has two main ways of processing the request and returning a response:
You can use either one of these approaches alone, or you can run them both together. A simplified description of the server-side Blazor pipeline:
The Controller pipeline is very similar:
If all you are ever doing is rendering pages server-side and interacting with them, then you can do this entirely through Blazor, with no need for controllers. This is not the main selling point of Blazor, server-side rendering has been around for a while. The interesting part is that you can change the hosting model to Client-side and still achieve this! The client maintains a connection to the server, and sends events when the user interacts with pages or components, but Blazor handles all the details for you. This does come with overhead - every time something on the UI needs to be updated, the server has to send that UI to the client. If you want to remove that overhead, and just send the raw data, then you would need to create api controllers.
I suggest reading through these pages for more information:
Upvotes: 7