dotnet / dotnet/aspnetcore

Blazor webassembly subpath doesn't work

Open
#51,929 11 comments 2 reactions 0 assignees View on GitHub
area-blazor Docs Needs: Attention :wave:
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I want to host a blazor webassembly under a subpath (http://foo.com/bar instead of http://foo.com)
I've tried to set it up using the official documentation https://learn.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/?view=aspnetcore-7.0&tabs=visual-studio#app-base-path

The project is hosted via asp.net core
![image](https://github.com/dotnet/aspnetcore/assets/1768336/985b6501-047a-40aa-a96f-85a27ea42d52)

In the webapplication project, i can set the subpath by using usePathBase, UseStaticFiles and UseBlazorFrameworkFiles
```csharp
var app = builder.Build();
app.UseBlazorFrameworkFiles("/bar");
app.UseStaticFiles("/bar");
app.UsePathBase("/bar");
```

In the wwwroot/index.html, i've set the base to:
```html

```

With those changes, some parts of the webassembly are available under the subpath, but **blazor.webassembly.js** is not. This file is loaded via the subpath, but cannot be provided.
![image](https://github.com/dotnet/aspnetcore/assets/1768336/23414c42-950a-408c-9b58-ee210799040c)

I think what is missing is the StaticWebAssetBasePath (https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-7.0#static-web-asset-base-path)
I've set this in the webassembly csproj file:
```xml
// I've tried with and without trailing slash
bar
bar/
```
As soon as i add the StaticWebAssetBasePath, blazro webassembly is not retrieved anymore. (Be it using bar/ or bar/index.html)
![image](https://github.com/dotnet/aspnetcore/assets/1768336/33b6e302-d9c5-4d39-9850-f92290ce939e)

The complete example is available on github: https://github.com/suntsu42/BlazorSubPathSample

Question: What do i need to configure to host a blazor webassembly project under a subpath?

### Expected Behavior

_No response_

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

net7.0

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the linked BlazorSubPathSample and review wwwroot/index.html alongside the ASP.NET Core pipeline using UseBlazorFrameworkFiles, UseStaticFiles, and UsePathBase. Check the webassembly project’s StaticWebAssetBasePath setting and the linked hosting documentation. Done means the application and blazor.webassembly.js load correctly when hosted under /bar/.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.