我使用 Visual Studio 2022 V17.8.2 中的模板创建 Blazor Web 应用程序。
我发布了它(Web 服务器 IIS - WebDeploy 包)并将其导入到 IIS 中名为
blazortest80
的“默认网站”下。
这是模板中的一些代码 -
program.cs
:
using BlazorWebAppNet8.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();
App.razor
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="BlazorWebAppNet8.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<HeadOutlet />
</head>
<body>
<Routes />
<script src="_framework/blazor.web.js"></script>
</body>
</html>
IISProfile.pubxml
<Project>
<PropertyGroup>
<WebPublishMethod>Package</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
<ExcludeApp_Data>false</ExcludeApp_Data>
<ProjectGuid>a943a358-ece1-407d-8ff4-834f46452867</ProjectGuid>
<DesktopBuildPackageLocation>C:\temp\testdeploy\BlazorWebAppNet8.zip</DesktopBuildPackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<DeployIisAppPath>blazortest80</DeployIisAppPath>
<_TargetId>IISWebDeployPackage</_TargetId>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
当我通过导航到
在浏览器中打开页面时https://testserver.de/blazortest80
我收到此错误,但没有任何效果:
我阅读了文档(Host And Deploy),他们提到要设置正确的基本路径。在这里我有点迷失 - 我不需要 Blazor Server 和 .NET 7.0。
所以我尝试了以下方法:
program.cs
- 添加一行代码:
var app = builder.Build();
app.UsePathBase("/blazortest80");
App.razor
- 删除一行:
<base href="/" />
当我现在用
https://testserver.de/blazortest80
打开页面时,我得到了相同的结果,但如果我在末尾添加 /
并转到 https://testserver.de/blazortest80/
,我的网站看起来不错。
我可以导航到
Counter
和 Weather
,但是如果我想导航到 Home
,它不起作用。
我也有一个大型 Blazor Server 应用程序,我将其迁移到 .NET 8.0 (
blazor.web.js
),并且我看到了相同的行为,其中 NavigationManager
无法导航到页面...
以防万一:在 Visual Studio 中运行时,无需 IIS Express 中的
.UsePathBase()
即可正常工作。
有什么建议吗?