You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在RCL中创建Blazor页面?访问异常及托管切换需求咨询

Is it possible to host pages in a Razor Class Library (RCL) for Blazor Server, with future WASM compatibility?

Absolutely, this approach is not only feasible—it’s a smart strategy for building a Blazor app that can smoothly transition between Server and WASM hosting models later on! Let’s break down why your test page isn’t loading right now, and walk through the exact steps to fix it.

Why your /testpage isn’t accessible

By default, Blazor Server’s routing system only scans the current project’s assembly for components with the @page directive. It doesn’t automatically pick up routable pages from referenced RCLs—you need to explicitly tell it to look in that external assembly.

Step-by-step fix

Using your reproduction scenario (new Blazor Server project + RCL with @page "/testpage" on Component1.razor), here’s how to get this working:

1. Verify your RCL project configuration

First, make sure your RCL is set up to support pages and components:

  • Open your RCL’s .csproj file.
  • Ensure you have this property (it’s usually added automatically if you checked "Support pages and views" when creating the RCL, but double-check):
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
    

This flag tells the framework that your RCL contains routable pages, not just reusable UI components.

2. Import the RCL’s namespace in your Blazor Server app

In your Blazor Server project’s _Imports.razor file, add a line to import your RCL’s root namespace:

@using YourRCLProjectName

This makes all components and pages from the RCL available to your app.

3. Update the Router to scan the RCL’s assembly

This is the critical missing step. Open your Blazor Server’s App.razor file, and modify the <Router> component to include your RCL’s assembly in the AdditionalAssemblies parameter:

<Router AppAssembly="@typeof(App).Assembly"
        AdditionalAssemblies="new[] { typeof(YourRCLProjectName.Component1).Assembly }">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

Replace YourRCLProjectName.Component1 with any component from your RCL—this tells the router to scan that entire assembly for routable pages.

4. Test the page

Restart your Blazor Server project, and navigate to /testpage—you should now see your RCL’s Component1 page loaded successfully!

Tips for future WASM compatibility

Your plan to use controllers in Blazor Server and have the RCL consume them via HTTP is perfect for a smooth transition to WASM:

  • Keep all shared components, pages, and business logic (that doesn’t depend on Server-specific APIs) in the RCL.
  • When you’re ready to switch to WASM:
    1. Reference the same RCL in your Blazor WASM project.
    2. Update the WASM app’s App.razor Router to include the RCL’s assembly (same step as above).
    3. Migrate your Blazor Server controllers to a standalone ASP.NET Core Web API project (or keep them if you’re using hosted WASM).
    4. Ensure the RCL’s HttpClient usage is configured to point to the correct API endpoint in both environments (use environment-specific settings for this).

Common pitfalls to avoid

  • Forgetting AdditionalAssemblies: This is the #1 reason RCL pages don’t load—Blazor won’t scan external assemblies unless you explicitly specify them.
  • Missing <AddRazorSupportForMvc>: Older .NET versions require this property to enable page support in RCLs.
  • Incorrect namespace imports: If your RCL components aren’t showing up at all, double-check the _Imports.razor line for typos.

内容的提问来源于stack exchange,提问作者Leonardo Lurci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:27:34