Will my simple approach to migrating a Razor page to a Blazor component, work?

Falanga, Rod, DOH 260 Reputation points
2024-12-09T17:16:03.36+00:00

I've got another Blazor question. I started an ASP.NET Razor application a couple months back, because as I understood it at the time, it was the best way of getting to use Windows Authentication. And I know that it is possible to host Blazor components within Razor pages. So far, it seems to be working.But I would like to take one of my Razor pages, and convert it to a Blazor component. I found a good Microsoft Learn course on this here: https://zcusa.951200.xyz/en-us/aspnet/core/blazor/forms/input-components?view=aspnetcore-8.0

And I had hoped all I'd have to do was rename the .cshtml file to .razor and I'd be done. But before I did that, I searched to see if my simple approach would work. It looks like it is more complicated then I thought. I found this page which describes migrating a Razor page to Blazor https://www.devgem.io/posts/step-by-step-guide-to-migrating-asp-net-core-razor-pages-to-blazor

So, is that second link, correct? Is the steps I need to do in order to convert my Razor page to a Blazor component?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,706 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,635 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,716 Reputation points
    2024-12-09T18:37:24.7066667+00:00

    the second link is just for converting a razor page to a Blazor page. to host a Blazor component on a razor page, is more complex:

    https://zcusa.951200.xyz/en-us/aspnet/core/blazor/components/integration?view=aspnetcore-9.0


  2. Ruikai Feng - MSFT 2,586 Reputation points Microsoft Vendor
    2024-12-10T03:20:11.6666667+00:00

    Hi,@Falanga, Rod, DOH

    So, is that second link, correct?

    The codes in second document to host razor components is not correct

    builder.Services.AddSignalR();
    builder.Services.AddServerSideBlazor();
    
    .....
    app.UseStaticFiles();
    app.UseRouting(); 
    
    app.UseBlazorFrameworkFiles(); 
    app.UseAuthorization(); 
    app.MapRazorPages(); 
    // don't remove if you still have Razor pages
    app.MapDefaultControllerRoute();
     app.MapBlazorHub();
    
    
    

    the services registered are for blazor server apps and the middleware app.UseBlazorFrameworkFiles(); is for blazor webassembly apps

    although there exists the route in test component:

    @page "/Test"

    The test component is not routeble at all.

    Based on Bruce's answer, notice the .net version of your app,in .net 7 or lower versions ,you need _host.cshtml for all your razor components to be rendered on ,in .net 8 or higher versions,you could use app.razor instead

    document for .net 7 and document for .net 8


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Ruikai Feng


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.