4 ms·
I really like the approach of Blazor wrt WASM, but really dislike having to dump whole code blocks into CSHTML files like I'm writing old ASP.NET Forms again.
by kirse 7y ago
I really like the approach of Blazor wrt WASM, but really dislike having to dump whole code blocks into CSHTML files like I'm writing old ASP.NET Forms again. Something just feels off.
Is there any way to isolate those code blocks and import them?
EDIT: To answer my own question, this appears to be the solution:
https://www.telerik.com/blogs/using-a-code-behind-approach-to-blazor-components https://www.telerik.com/blogs/using-a-code-behind-approach-t...
- pjmlp 7y agoWell there is Blazor components, unless you mean something else.
- SigmundA 7y agoBlazor supports components: https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1 https://docs.microsoft.com/en-us/aspnet/core/blazor/componen...
- angrysaki 7y agoASP.NET Core 3.1 added support for partial classes for Razor Components, so I don't think you need to use the inheritance style code behind anymore. https://gunnarpeipman.com/blazor-partial-page-class/ https://gunnarpeipman.com/blazor-partial-page-class/
- bob1029 7y agoThis is something that hosed me up too at first when using Blazor. There is a specific item type in the VS "Add New Item" dialog that is called "Razor Component" with a fancy purple icon next to it. If your files in the solution explorer end in .razor, you are using the correct item type. If your files end in .cshtml, you are going to have a bad time and everything will seem very wrong. At first, I was using the "Add->Razor Page" context menu option assuming this was what I wanted, but it will give you the wrong type of item.
- manigandham 7y agoThe latest release (.Net 3.1) finished the partial classes implementation so now you can create a `Comp.razor` template file and a separate `Comp.razor.cs` code-behind file that only contains the code. These are automatically compiled together on build.