Christian
Christian

Reputation: 1090

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

Upvotes: 5

Views: 3948

Answers (2)

Developer Dude
Developer Dude

Reputation: 67

I had this issue, and needed to reboot Visual Studio... If it's weird with a beard, turn it off and turn it back on.

Upvotes: 1

Ryan
Ryan

Reputation: 20106

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

Upvotes: 6

Related Questions