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:
- .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>
- Startup.cs, Configure method:
public void Configure(IApplicationBuilder app)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
});
}
- Startup.cs ConfigureService method:
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddServerSideBlazor();
}
- Layout.cshtml
<base href="~/" />
<script src="_framework/blazor.server.js"></script>
- Index.cshtml
@(await Html.RenderComponentAsync<Nop.Plugin.Test.Web.Themes.Views.Shared.RazorComponents.Component>(RenderMode.ServerPrerendered, new { Data = " Hello World " }))