convert-blazor-server-to-webapp
它能做什么
这项技能把较旧的 Blazor Server 应用转换为 .NET 8 或更高版本中较新的 Blazor Web App 模型。适合拥有旧版 Blazor Server 应用、希望迁移到当前托管模型的 .NET 团队。
适用场景
- ✓当你的 Blazor Server 应用运行在 .NET 6 或 .NET 7 上,需要迁移到 .NET 8+ 时。
- ✓当你的应用仍在使用旧版 Blazor Server 的 hub 和宿主页面设置时。
- ✓当你希望在保留交互式服务器渲染的同时使用新的 Blazor Web App 功能的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-aspnetcore/skills/convert-blazor-server-to-webapp/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把我的 .NET 7 Blazor Server 应用转换为 .NET 8 上的 Blazor Web App,并保持其交互性。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会更新托管设置,用根组件替换旧的宿主页面,更新脚本和服务,并保留交互式服务器渲染,使行为保持不变。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Convert Blazor Server App to Blazor Web App
This skill helps an agent convert a pre-.NET 8 Blazor Server app into a .NET 8+ Blazor Web App. The old hosting model uses AddServerSideBlazor/MapBlazorHub with a _Host.cshtml Razor Page as the entry point. The new Blazor Web App model uses AddRazorComponents/MapRazorComponents with an App.razor root component, enabling per-component render modes, enhanced navigation, streaming rendering, and other .NET 8+ features. The converted app uses InteractiveServer render mode to preserve existing interactive behavior.
When to Use
- Migrating a Blazor Server app from .NET 6 or .NET 7 to .NET 8+
- App currently uses
AddServerSideBlazor()andMapBlazorHub()inProgram.cs(orStartup.cs) - App uses
Pages/_Host.cshtml(or_Host.razor) as the host page with Component Tag Helpers - Want to adopt new Blazor Web App features while keeping interactive server rendering
When Not to Use
- The app already uses
AddRazorComponentsandMapRazorComponents. It is already a Blazor Web App — no conversion is needed. Stop here and tell the user the app is already using the Blazor Web App model. - Blazor WebAssembly or hosted Blazor WebAssembly app — these have a different migration path
- The app should stay on the legacy Blazor Server hosting model (just update TFM and packages)
- The app targets .NET Framework — it must be migrated to .NET first
Inputs
| Input | Required | Description |
|---|---|---|
| Blazor Server project | Yes | The .csproj and source files of the Blazor Server app |
| Target framework | Yes | .NET 8 or later (e.g., net8.0, net9.0, net10.0) |
Program.cs or Startup.cs |
Yes | The app's service and middleware configuration |
_Host.cshtml location |
Recommended | Usually Pages/_Host.cshtml; may be _Host.razor in some projects |
Workflow
Commit strategy: Commit after each logical step so the migration is reviewable and bisectable.
Step 1: Update the project file
Update the .csproj file:
- Change the Target Framework Moniker (TFM) to the target version:
<TargetFramework>net8.0</TargetFramework> - Update all
Microsoft.AspNetCore.*,Microsoft.EntityFrameworkCore.*,Microsoft.Extensions.*, andSystem.Net.Http.Jsonpackage references to the matching version.
For non-Blazor project file changes (nullable reference types, implicit usings, HTTP/3 support, etc.), see the general ASP.NET Core migration guide.
Step 2: Create Routes.razor from App.razor
The old App.razor contains the <Router> component. This content moves to a new Routes.razor file so that App.razor can become the root HTML document component.
- Create a new file
Routes.razorin the project root. - Move the entire content of
App.razorintoRoutes.razor. - If the content is wrapped in
<CascadingAuthenticationState>, remove that wrapper (it will be replaced by a service in Step 5). - Leave
App.razorempty for the next step.
The resulting Routes.razor should look similar to:
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
If the app uses <AuthorizeRouteView> instead of <RouteView>, keep it — it works the same way in Blazor Web Apps.
Step 3: Convert _Host.cshtml to App.razor
Move the HTML shell from Pages/_Host.cshtml into the now-empty App.razor and transform it from a Razor Page into a Razor component:
Remove Razor Page directives — delete
@page "/",@using Microsoft.AspNetCore.Components.Web,@namespace, and@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers.Add component injection — if using environment-conditional error UI, add:
@inject IHostEnvironment EnvFix the base tag — replace
<base href="~/" />with<base href="/" />.Replace HeadOutlet Component Tag Helper — replace:
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />with:
<HeadOutlet @rendermode="InteractiveServer"
常见问题
它会保持应用的交互性吗?
会。转换后的应用使用交互式服务器渲染,以保持当前的行为。
它涵盖已经转换过的应用吗?
不涵盖。如果应用已经使用 Blazor Web App 模型,就不需要转换。
它涵盖 WebAssembly 应用吗?
不涵盖。Blazor WebAssembly 应用有不同的迁移路径。
详情
作者的原始描述
Guides conversion of a pre-.NET 8 Blazor Server app into a .NET 8+ Blazor Web App. USE FOR: migrating apps that use AddServerSideBlazor and MapBlazorHub to the AddRazorComponents/MapRazorComponents model, converting _Host.cshtml to an App.razor root component, replacing blazor.server.js with blazor.web.js, migrating CascadingAuthenticationState to a service, adopting new Blazor Web App features like enhanced navigation and streaming rendering. DO NOT USE FOR: apps that are already Blazor Web Apps (already use AddRazorComponents and MapRazorComponents), Blazor WebAssembly or hosted Blazor WebAs
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14559
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。