Figuring out where to put the code
We have seen examples of writing code directly in the Razor file. I prefer doing that unless the code gets too complicated. I always lean in favor of readability.
There are four ways we can write our components:
- In the Razor file
- In a partial class
- Inheriting a class
- Only code
Let’s go through each item on this list in more detail.
In the Razor file
If we are writing a file that is not that complex, it would be nice not to switch files when writing components. As we already covered in this chapter, we can use the @code
directive to add code directly to our Razor file.
If we want to move the code to a code-behind file, then it is only the directives that we need to change. For the rest of the code, we can just move to the code-behind class. When I started with Blazor, writing code and markup in the same file felt strange, but I suggest you try it out when developing your web apps.
At...