You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Blazor has case sensitive URLs when hosted in a Child Directory #6818
Changing the base href in the index.html of the Blazor App:
base hef="/dashboard/"
However, if you do this you can only use the casing of the base href to access your Blazor pages. For example /dashboard/counter and /dashboard/Counter will work but /Dashboard/counter will not. That is you must always specify 'dashboard' (in this example) within the URL in lower case. This is a behavior difference to when the Blazor app is hosted in the root directory, where all urls are case insensitive.
The error you get is:
warn: Microsoft.AspNetCore.Blazor.Server.BlazorHub[0]
Unhandled Server-Side exception
System.AggregateException: One or more errors occurred. (Error: No element is currently associated with component 1) ---> Microsoft.AspNetCore.Blazor.Browser.Rendering.RemoteRendererException: Error: No element is currently associated with component 1
--- End of inner exception stack trace ---
Reacted by rborosak, Saeb Amini, Jon Tvermose Nielsen, Jimmy Gilles, marcOcram, radek7210, Fabio Silva, Ryan Watson, rrajewski, Andy and 2 more
Temporary fix: Put this in the index.html, above the blazor script tag:
<script>
var path = window.location.pathname.split('/');
var baseTag = document.getElementsByTagName('base');
baseTag[0].setAttribute('href', '/' + path[1] + '/');
</script>
Reacted by Mike Wodarczyk, Saeb Amini, Jon Tvermose Nielsen, John Britton, Tim Conner, Fabio Silva, stevepATqc, Milen Simeonov, hkelectric-apj-052d and Ethan
It's really sad that Microsoft hasn't addressed this issue yet.
Here is a solution to the problem for those using .NET 8 Server Side Blazor. For WASM, I think you would need to do something in JavaScript, however, I do not have this problem in WASM like I do in Server Side.
Add in a setting to the appsettings.json like so:
{
"BASE_PATH": "/app"
}
Add in a class that will programmatically set the base path in App.razor.
public class BlazorQualityControlService
{
IConfiguration Configuration;
IHttpContextAccessor HttpContextAccessor;
public BlazorQualityControlService(IHttpContextAccessor accessor, IConfiguration configuration)
{
HttpContextAccessor = accessor;
Configuration = configuration;
}
public string GetBasePath()
{
// If the base path was set, find it in the base URI.
if (Configuration["BASE_PATH"] != null)
{
// Find the part of the request URI that matches the base path using case insensitive matching.
string basePath = Configuration["BASE_PATH"] ?? "";
string requestPath = HttpContextAccessor.HttpContext?.Request.GetDisplayUrl() ?? "";
int index = requestPath.IndexOf(basePath, StringComparison.OrdinalIgnoreCase);
// Grab the case-sensitive base path from the request URI.
string qualityControlBasePath = basePath;
if (index >= 0)
{
qualityControlBasePath = requestPath.Substring(index, basePath.Length);
}
return qualityControlBasePath + "/";
}
else
{
return "/";
}
}
}
Add in the service in your start up:
builder.Services.AddTransient();
Update the App.razor to use the service:
It's really sad that Microsoft hasn't addressed this issue yet.
Here is a solution to the problem for those using .NET 8 Server Side Blazor. For WASM, I think you would need to do something in JavaScript, however, I do not have this problem in WASM like I do in Server Side.
Add in a setting to the appsettings.json like so:
{
"BASE_PATH": "/app"
}
Add in a class that will programmatically set the base path in App.razor.
public class BlazorQualityControlService
{
IConfiguration Configuration;
IHttpContextAccessor HttpContextAccessor;
public BlazorQualityControlService(IHttpContextAccessor accessor, IConfiguration configuration)
{
HttpContextAccessor = accessor;
Configuration = configuration;
}
public string GetBasePath()
{
// If the base path was set, find it in the base URI.
if (Configuration["BASE_PATH"] != null)
{
// Find the part of the request URI that matches the base path using case insensitive matching.
string basePath = Configuration["BASE_PATH"] ?? "";
string requestPath = HttpContextAccessor.HttpContext?.Request.GetDisplayUrl() ?? "";
int index = requestPath.IndexOf(basePath, StringComparison.OrdinalIgnoreCase);
// Grab the case-sensitive base path from the request URI.
string qualityControlBasePath = basePath;
if (index >= 0)
{
qualityControlBasePath = requestPath.Substring(index, basePath.Length);
}
return qualityControlBasePath + "/";
}
else
{
return "/";
}
}
}
Add in the service in your start up:
builder.Services.AddTransient();
Update the App.razor to use the service: @Inject BlazorQualityControlService BlazorQualityControlService
Additionally, I also have the following set:
if (builder.Configuration["BASE_PATH"] != null)
{
app.UsePathBase(builder.Configuration["BASE_PATH"]);
}
FYI, I place this app.UsePathBase after app.UseRouting().
I tested this with both setting a BASE_PATH and not setting a BASE_PATH and it seems to work for me. I hope this helps!
This way is worked on .Net 8
But When I upgrade to .Net 9
Blazor.web.js is break.
it can't relly to use blazor.web.js work
Had the same problem today, to fix this I did the following:
+ public class NavigationManagerOptions
+ {
+ /// <summary>
+ /// Gets or sets the string comparison used when comparing URIs against the base URI.
+ /// Default is <see cref="StringComparison.Ordinal"/> for backward compatibility.
+ /// Set to <see cref="StringComparison.OrdinalIgnoreCase"/> to enable case-insensitive matching.
+ /// </summary>
+ public StringComparison PathBaseComparison { get; set; } = StringComparison.Ordinal;
+ }
+ Direct fix at the root: affects all Blazor navigations - Slightly more abstract for developers (“why do I have to change NavigationManager if my issue is connected with Mapping?”).
Alternative:
A more intuitive place from the developer perspective would be setting that option during mapping:
That would requite extending the existing MapOptions class with PathBaseComparison:
public class MapOptions
{
+ /// <summary>
+ /// Gets or sets the string comparison used when comparing URIs against the base URI.
+ /// Default is <see cref="StringComparison.Ordinal"/> for backward compatibility.
+ /// Set to <see cref="StringComparison.OrdinalIgnoreCase"/> to enable case-insensitive matching.
+ /// </summary>
+ public StringComparison PathBaseComparison { get; set; } = StringComparison.Ordinal;
}
- Might lead to inconsistencies between different mapping styles (Map is not the only way, developer can use e.g. MapWhen, UsePathBase).
I'm leaning towards the 1st option and I will post a PR with the proposal soon but I'm open to suggestions.
Line 293
in
[27fe84d](/dotnet/aspnetcore/commit/27fe84d1b17e218c66b53351799687aec7a7598d)
if (MemoryExtensions.StartsWith(uri, _baseUri!.OriginalString.AsSpan(), StringComparison.Ordinal))
Line 302
in
[27fe84d](/dotnet/aspnetcore/commit/27fe84d1b17e218c66b53351799687aec7a7598d)
if (_baseUri.OriginalString.EndsWith('/') && MemoryExtensions.Equals(uriPathOnly, _baseUri.OriginalString.AsSpan(0, _baseUri.OriginalString.Length - 1), StringComparison.Ordinal))
Line 267
in
[27fe84d](/dotnet/aspnetcore/commit/27fe84d1b17e218c66b53351799687aec7a7598d)
if (uri.StartsWith(_baseUri!.OriginalString, StringComparison.Ordinal))
Line 276
in
[27fe84d](/dotnet/aspnetcore/commit/27fe84d1b17e218c66b53351799687aec7a7598d)
if (_baseUri.OriginalString.EndsWith('/') && uriPathOnly.Equals(_baseUri.OriginalString.AsSpan(0, _baseUri.OriginalString.Length - 1), StringComparison.Ordinal))
Line 558
in
[27fe84d](/dotnet/aspnetcore/commit/27fe84d1b17e218c66b53351799687aec7a7598d)
if (uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal))
Line 568
in
[27fe84d](/dotnet/aspnetcore/commit/27fe84d1b17e218c66b53351799687aec7a7598d)
if (baseUri.OriginalString.EndsWith('/') && uriPathOnly.Equals(baseUri.OriginalString.AsSpan(0, baseUri.OriginalString.Length - 1), StringComparison.Ordinal))
API proposal:
Because the issue is strictly bound to NavigationManager behavior, we can consider fixing it in its options. Overriding would look like:
+ public class NavigationManagerOptions
+ {
+ /// <summary>
+ /// Gets or sets the string comparison used when comparing URIs against the base URI.
+ /// Default is <see cref="StringComparison.Ordinal"/> for backward compatibility.
+ /// Set to <see cref="StringComparison.OrdinalIgnoreCase"/> to enable case-insensitive matching.
+ /// </summary>
+ public StringComparison PathBaseComparison { get; set; } = StringComparison.Ordinal;
+ }
+ Direct fix at the root: affects all Blazor navigations - Slightly more abstract for developers (“why do I have to change NavigationManager if my issue is connected with Mapping?”).
Alternative:
A more intuitive place from the developer perspective would be setting that option during mapping:
That would requite extending the existing MapOptions class with PathBaseComparison:
public class MapOptions
{
+ /// <summary>
+ /// Gets or sets the string comparison used when comparing URIs against the base URI.
+ /// Default is <see cref="StringComparison.Ordinal"/> for backward compatibility.
+ /// Set to <see cref="StringComparison.OrdinalIgnoreCase"/> to enable case-insensitive matching.
+ /// </summary>
+ public StringComparison PathBaseComparison { get; set; } = StringComparison.Ordinal;
}
- Might lead to inconsistencies between different mapping styles (Map is not the only way, developer can use e.g. MapWhen, UsePathBase).
I'm leaning towards the 1st option and I will post a PR with the proposal soon but I'm open to suggestions.
This is not done casually. It's intended. Paths are case sensitive and can't be compared in a case-insensitive way. It has security implications https://www.rfc-editor.org/rfc/rfc3986.
This is not done casually. It's intended. Paths are case sensitive and can't be compared in a case-insensitive way. It has security implications https://www.rfc-editor.org/rfc/rfc3986.
The URL though is case insensitive, as outlined in rfc3986. So if the concern is around resolving non-case sensitive URLs to case sensitive paths, there should be a mapping option for us to implement. Preferably something more robust and standardized than hacking redirects or overriding the base path.
The URL though is case insensitive, as outlined in rfc3986. So if the concern is around resolving non-case sensitive URLs to case sensitive paths, there should be a mapping option for us to implement. Preferably something more robust and standardized than hacking redirects or overriding the base path.
@javiercn's concerns, including the implications of this change on 3rd party libraries are valid. As a middle ground, we are considering building a component that would be an equivalent of the following user code:
Activity
Add event names for logs (dotnet#6818) (dotnet#4495)
RealTadango commentedon Aug 12, 2019
Temporary fix: Put this in the index.html, above the blazor script tag:
70 remaining items
Edemilorhea commentedon Dec 10, 2024
And
This way is worked on .Net 8
But When I upgrade to .Net 9
Blazor.web.js is break.
it can't relly to use blazor.web.js work
philip-pandoscape commentedon Dec 11, 2024
@Edemilorhea I could be wrong, but the error does not seem related.
m4thox commentedon Apr 8, 2025
Had the same problem today, to fix this I did the following:
Added on Program.cs:
App.razor.cs:
@Inject IConfiguration Configuration
Replaced the base tag with:
Hope this helps you as well.
ilonatommy commentedon Nov 28, 2025
The core of the problem here is
StringComparisonoptions we are using for urls in blazor'sNavigationManager, so following places:aspnetcore/src/Components/Components/src/NavigationManager.cs
Line 293 in 27fe84d
aspnetcore/src/Components/Components/src/NavigationManager.cs
Line 302 in 27fe84d
aspnetcore/src/Components/Components/src/NavigationManager.cs
Line 267 in 27fe84d
aspnetcore/src/Components/Components/src/NavigationManager.cs
Line 276 in 27fe84d
aspnetcore/src/Components/Components/src/NavigationManager.cs
Line 558 in 27fe84d
aspnetcore/src/Components/Components/src/NavigationManager.cs
Line 568 in 27fe84d
API proposal:
Because the issue is strictly bound to
NavigationManagerbehavior, we can consider fixing it in its options. Overriding would look like:That would require a new API, e.g.
+Direct fix at the root: affects all Blazor navigations-Slightly more abstract for developers (“why do I have to change NavigationManager if my issue is connected with Mapping?”).Alternative:
A more intuitive place from the developer perspective would be setting that option during mapping:
That would requite extending the existing
MapOptionsclass with PathBaseComparison:-Might lead to inconsistencies between different mapping styles (Mapis not the only way, developer can use e.g.MapWhen,UsePathBase).I'm leaning towards the 1st option and I will post a PR with the proposal soon but I'm open to suggestions.
SPWizard01 commentedon Nov 29, 2025
I would lean towards 1st option as I do not see the point of having different string comparisons for different routes.
javiercn commentedon Dec 1, 2025
This is not done casually. It's intended. Paths are case sensitive and can't be compared in a case-insensitive way. It has security implications https://www.rfc-editor.org/rfc/rfc3986.
ThatRickGuy commentedon Dec 1, 2025
The URL though is case insensitive, as outlined in rfc3986. So if the concern is around resolving non-case sensitive URLs to case sensitive paths, there should be a mapping option for us to implement. Preferably something more robust and standardized than hacking redirects or overriding the base path.
ilonatommy commentedon Dec 2, 2025
@javiercn's concerns, including the implications of this change on 3rd party libraries are valid. As a middle ground, we are considering building a component that would be an equivalent of the following user code:
That would solve the case for blazor web. For standalone webassembly, a workaround like
#6818 (comment) would still be needed.