Introduction


I have written various articles on C# Corner about Blazor and this time I want to write an article about combining VB.NET and C#. I have found that many big companies are still using Webforms with VB.NET and if they want to migrate their application to Blazor in the future, it is possible. That is the reason for writing this article.

Create Blazor Server Project in Visual Studio 2019


Please use Visual Studio 2019 to create a Blazor server project
Combine C# And VB.NET In Same Blazor Project
You can see the project structure which is in C#.
Combine C# And VB.NET In Same Blazor Project
Now, we can add a new VB.NET Class library project to the solution.
Combine C# And VB.NET In Same Blazor Project
We can quickly add an Employee class in VB.NET project and add two properties as shown below.
Combine C# And VB.NET In Same Blazor Project
We can add this VB.NET library to our existing Blazor server project.
Add a new component, Employees, inside the “Pages” folder of Blazor server project.
Add the below code inside the component page.
Employees.razor
  1. @using VBNETClassLibrary
  2. @page "/employees"
  3. <h3>Employee List</h3>
  4. @if (employees == null)
  5. {
  6. <p>Loading...</p>
  7. }
  8. else
  9. {
  10. <table class='table'>
  11. <thead>
  12. <tr>
  13. <th>Name</th>
  14. <th>Age</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. @foreach (var employee in employees)
  19. {
  20. <tr>
  21. <td>@employee.Name</td>
  22. <td>@employee.Age</td>
  23. </tr>
  24. }
  25. </tbody>
  26. </table>
  27. }
  28. @code {
  29. List<Employee> employees = new List<Employee>();
  30. protected override void OnInitialized()
  31. {
  32. employees.Add(new Employee { Name = "Sarathlal Saseendran", Age = 38 });
  33. employees.Add(new Employee { Name = "Anil Soman", Age = 43 });
  34. }
  35. }
You can notice that we have used VB.NET class library inside the component.
Combine C# And VB.NET In Same Blazor Project
We can modify the shared component NavMenu by adding the Employees routing.
NavMenu.razor
  1. <div class="top-row pl-4 navbar navbar-dark">
  2. <a class="navbar-brand" href="">BlazorCsharpVB</a>
  3. <button class="navbar-toggler" @onclick="ToggleNavMenu">
  4. <span class="navbar-toggler-icon"></span>
  5. </button>
  6. </div>
  7. <div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
  8. <ul class="nav flex-column">
  9. <li class="nav-item px-3">
  10. <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
  11. <span class="oi oi-home" aria-hidden="true"></span> Home
  12. </NavLink>
  13. </li>
  14. <li class="nav-item px-3">
  15. <NavLink class="nav-link" href="counter">
  16. <span class="oi oi-plus" aria-hidden="true"></span> Counter
  17. </NavLink>
  18. </li>
  19. <li class="nav-item px-3">
  20. <NavLink class="nav-link" href="fetchdata">
  21. <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
  22. </NavLink>
  23. </li>
  24. <li class="nav-item px-3">
  25. <NavLink class="nav-link" href="employees">
  26. <span class="oi oi-list-rich" aria-hidden="true"></span> Employees
  27. </NavLink>
  28. </li>
  29. </ul>
  30. </div>
  31. @code {
  32. private bool collapseNavMenu = true;
  33. private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
  34. private void ToggleNavMenu()
  35. {
  36. collapseNavMenu = !collapseNavMenu;
  37. }
  38. }
We can run the application:
Combine C# And VB.NET In Same Blazor Project
We have successfully run the application.

Conclusion


In this post, we have seen how to combine VB.NET with a Blazor Server project which is in C#. The intention for this application is to motivate people to do migration from previous VB.NET webforms applications to the current Blazor framework.