Integrate Blazor components with Sdk Microsoft.Net.Sdk in the project type "class library"

58 Views Asked by At

Is it possible to integrate Blazor components in the Microsoft.Net.Sdk, class library project. Target: net7.0. It is a custom plugin in Nocommerce framework.

if I try with the new MVC Web project (Microsoft.NET.Sdk.Web), everything is working perfectly.

Steps I did:

  1. .csproj files:
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <AssemblyName>Nop.Plugin.Test</AssemblyName>
        <RootNamespace>Nop.Plugin.Test</RootNamespace>
        <Company>Test</Company>
        <OutputPath>..\..\..\..\test\src\Presentation\Nop.Web\Plugins\Test.Web</OutputPath>
        <OutDir>$(OutputPath)</OutDir>
        <OutNopWeb>$(OutDir)\..\..</OutNopWeb>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
        <NoWarn>1701;1702;IL2121;NETSDK1138</NoWarn>
    </PropertyGroup>
    
    <ItemGroup>
          <PackageReference Include="Microsoft.AspNetCore.Components" Version="7.0.0" />
          <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.0" />

    </ItemGroup>
    
    <PropertyGroup>
        <RazorLangVersion>7.0</RazorLangVersion>
    </PropertyGroup>

    <ItemGroup>
        <!-- Include your Blazor components here -->
        <RazorCompile Include="$(OutNopWeb)\Themes\Test\**\*.razor" />
    </ItemGroup>
</Project>
  1. Startup.cs, Configure method:
    public void Configure(IApplicationBuilder app)
    {
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapBlazorHub();
        });
}
  1. Startup.cs ConfigureService method:
    public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services.AddServerSideBlazor();
}
  1. Layout.cshtml
    <base href="~/" />
    <script src="_framework/blazor.server.js"></script>
  1. Index.cshtml
@(await Html.RenderComponentAsync<Nop.Plugin.Test.Web.Themes.Views.Shared.RazorComponents.Component>(RenderMode.ServerPrerendered, new { Data = " Hello World " }))
0

There are 0 best solutions below