Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net core 3.1 can't find Razor component to render

Im trying to integrate Blazor into an existing asp.net core 3.1 application. All tutorials I have seen, says that after doing the correct setup in the web project, you should be able to do this in any cshtml file:

<component>
    @(await Html.RenderComponentAsync<HelloComponent>(RenderMode.ServerPrerendered))
</component>

But instead I get this: enter image description here

The type or namespace 'HelloComponent' could not be found.

What I have done

1) Added following to my Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddServerSideBlazor();
    // .. removed other services...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRouting();            
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapControllers();
        endpoints.MapRazorPages();
        endpoints.MapBlazorHub();
    });
    // .. removed the rest of configuration..
}

2) Added _Imports.razor file to /Pages folder

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop    
@using WebApp.Pages.Shared.Components // The location of my HelloComponent

3) Added new Razor Component that contains only some text.

enter image description here

4) Added to _Layout.cshtml

<base href="~/" /> // In header
<script src="_framework/blazor.server.js"></script> // In bottom script section
like image 411
Christian Avatar asked Dec 09 '19 14:12

Christian


1 Answers

You also need to add references to Components folder in Pages/_ViewImports.cshtml

@using WebApp

@using WebApp.Pages.Shared.Components

@namespace WebApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
like image 51
Ryan Avatar answered Oct 18 '22 19:10

Ryan