skip to Main Content

I create a Blazor web app using the template from Visual Studio 2022 V17.8.2.

I published it (Web Server IIS – WebDeploy Packages) and imported it in IIS under the "Default Web Site" named blazortest80.

Here is some code from the template – 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:temptestdeployBlazorWebAppNet8.zip</DesktopBuildPackageLocation>
    <PackageAsSingleFile>true</PackageAsSingleFile>
    <DeployIisAppPath>blazortest80</DeployIisAppPath>
    <_TargetId>IISWebDeployPackage</_TargetId>
    <TargetFramework>net8.0</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>false</SelfContained>
  </PropertyGroup>
</Project>

When I open my page in my browser by navigating to

https://testserver.de/blazortest80 

I got this error and nothing works:

Website when hosting in IIS

I read in the docs (Host And Deploy) and there they mentioned to set the right base path. Here I’m somewhat lost – I did not need this with Blazor Server and .NET 7.0.

So I tried the following:

program.cs – add one line of code:

var app = builder.Build();

app.UsePathBase("/blazortest80");

App.razor – remove one line:

<base href="/" />

When I open the page now with https://testserver.de/blazortest80, I I got the same results, but if I add a / at the end and go to https://testserver.de/blazortest80/, I got the website looking good.

I can navigate to Counter and Weather, BUT if I want to navigate to Home, it does not work.

I got a big Blazor Server app too, which I migrated to .NET 8.0 (blazor.web.js) and I see the same behavior, where the NavigationManager can not navigate to pages…

Just in case: it all works without the .UsePathBase() in IIS Express when running it in Visual Studio.

Any advice?

2

Answers


  1. You could just resolve the issue by setting the base path as suggested below:

     <base href="/blazortest80/" />
    

    Below is the snapshot from my site:

    enter image description here

    Login or Signup to reply.
  2. But when we try to route /Blazortest80 or /BLAZORTEST80 same error even if we add href="/blazortest80/"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search