Blazor (Server) - Good Practice: Do i need a Controllers or is anything handled by Services

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..


Do i misunderstand or oversee something?

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.


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:

  • Pages (Blazor)
  • Controllers (e.g. MVC, or api controllers)

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:

  • Decide which page the request is routed to
  • Deserialize the body and query parameters, based on the C# class for that page
  • Instantiate the page
  • Execute the handler method on the page
  • Render the page
  • Return the rendered page as a response

The Controller pipeline is very similar:

  • Decide which controller+action the request is routed to
  • Deserialize the body and query parameters, based on the C# controller and action
  • Instantiate the controller
  • Execute the action (this might involve rendering a page)
  • Return the result of the action (this could be html, or it could be json)

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:

  • Architecture comparison
  • Hosting models