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:
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.
4) Added to _Layout.cshtml
<base href="~/" /> // In header
<script src="_framework/blazor.server.js"></script> // In bottom script section
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With