JackJack
JackJack

Reputation: 87

Publish Blazor Server-Side .Net 8 To IIS

recently I've tried publish my first Blazor Server-Side to IIS but it didn't work like I expected. I've created a folder inetpub\wwwroot\testPublish and then made an application (right click) Default Web Site and navigate the physical path to the created folder earlier. After that I publish my solution from the Visual Studio to the setup above.

When I tried to browse 'myservername'/testPublish this happened Screenshot

What am I missing here?

All my page when I tried to navigate it skip the testPublish it directly goes e.g'myservername'/home. When I added 'myservername'/testPublish/home then only it works. Including a function that lead to a controller it become 404 - Page not found.

Program.cs

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.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

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="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet">

    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
    <link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />

    <link rel="stylesheet" href="myPage.styles.css" />
    <link rel="stylesheet" href="app.css" />
    <link rel="icon" type="image/png" href="favicon.png" />
    <HeadOutlet @rendermode="@InteractiveServer" />
</head>

<body>
    <Routes @rendermode="@InteractiveServer" />

    <script src="_framework/blazor.web.js"></script>
    <script src="_content/MudBlazor/MudBlazor.min.js"></script>
</body>

</html>

My solution consist of services, controllers and components

Upvotes: 3

Views: 3715

Answers (1)

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22495

recently I've tried publish my first Blazor Server-Side to IIS but it didn't work like I expected. I've created a folder inetpub\wwwroot\testPublish and then made an application (right click) Default Web Site and navigate the physical path to the created folder earlier. After that I publish my solution from the Visual Studio to the setup above

Well, deploying blazor server app in IIS is pretty straightforward. And according to your description it seems correct. However, you haven't showed us how did you created the site in IIS within the application pool and if you have meet the prerequisites accordingly.

enter image description here

In my test I have done that in following steps and working accoordingly.

  1. Published in local folder:

Published your blazor server app from visual studio to any folder. That should look like below:

enter image description here

enter image description here

  1. Create Directory in inetpub under wwwroot

You should create new folder within the wwwroot under inetpub. That should look like as following:

enter image description here

  1. Copy and Paste all your published items here from the initial publish folder:

enter image description here

  1. Create new site under IIS Application pool:

enter image description here

enter image description here

  1. Map your inetpub wwwroot folder with newly created site:

enter image description here

Output:

enter image description here

enter image description here

Note: Please assign a port which has been occupied before. In addition, please refer to this official document for any further details.

Upvotes: 4

Related Questions