Skip to content

Blazor has case sensitive URLs when hosted in a Child Directory #6818

Closed

Description

@RodDaSilva

(NOTE: this report is in the context of "server-side" Blazor app hosting)

It is possible to host Blazor in a child directory by:

  1. Adding an app.Map() in the Startup.Configure() method of the host:

app.Map("/dashboard", child => { child.UseServerSideBlazor<DiskBounty.Dashboard.Startup>(); });

and

  1. 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 ---

Activity

  1. added
    area-mvc
    on Jan 18, 2019
  2. added
    area-blazor
    on Feb 6, 2019
  3. added
    bug
    on Mar 15, 2019
  4. added this to the 3.0.0-preview6 milestone on Mar 15, 2019
  5. removed
    area-mvc
    on May 9, 2019
  6. RealTadango commented on Aug 12, 2019

    @RealTadango

    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>
    
  7. removed this from the 3.1.0 milestone on Aug 12, 2019
  8. 70 remaining items

  9. Edemilorhea commented on Dec 10, 2024

    @Edemilorhea

    And

    1. It's really sad that Microsoft hasn't addressed this issue yet.
    2. 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.

    Image

    it can't relly to use blazor.web.js work

  10. philip-pandoscape commented on Dec 11, 2024

    @philip-pandoscape

    @Edemilorhea I could be wrong, but the error does not seem related.

  11. added
    Priority:3
    and removed
    Priority:2
    on Jan 13, 2025
  12. m4thox commented on Apr 8, 2025

    @m4thox

    And

    1. It's really sad that Microsoft hasn't addressed this issue yet.
    2. 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.

    Image

    it can't relly to use blazor.web.js work

    Had the same problem today, to fix this I did the following:

    Added on Program.cs:

    StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
    
    string? BASE_PATH = app.Configuration.GetValue<string>("BASE_PATH") ?? null;
    
    if (!string.IsNullOrWhiteSpace(BASE_PATH))
    {
        app.UsePathBase(new PathString(BASE_PATH));
    }
    
    app.MapStaticAssets();
    
    app.UseRouting();
    
    app.UseAntiforgery();
    
    

    App.razor.cs:

    @Inject IConfiguration Configuration

    Replaced the base tag with:

    <base href="@(Configuration.GetValue<string>("BASE_PATH"))" />
    

    Hope this helps you as well.

  13. ilonatommy commented on Nov 28, 2025

    @ilonatommy
    Member

    The core of the problem here is StringComparison options we are using for urls in blazor's NavigationManager, so following places:

    API proposal:

    Because the issue is strictly bound to NavigationManager behavior, we can consider fixing it in its options. Overriding would look like:

    builder.Services.Configure<NavigationManagerOptions>(options =>
    {
        options.PathBaseComparison = StringComparison.OrdinalIgnoreCase;
    });
    

    That would require a new API, e.g.

    + 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:

    app.Map("/dashboard", child => { ... }, new MapOptions 
    { 
        PathBaseComparison = StringComparison.OrdinalIgnoreCase
    });
    

    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.

  14. self-assigned this
    on Nov 28, 2025
  15. SPWizard01 commented on Nov 29, 2025

    @SPWizard01

    I would lean towards 1st option as I do not see the point of having different string comparisons for different routes.

  16. javiercn commented on Dec 1, 2025

    @javiercn
    Member

    The core of the problem here is StringComparison options we are using for urls in blazor's NavigationManager, so following places:

    API proposal:

    Because the issue is strictly bound to NavigationManager behavior, we can consider fixing it in its options. Overriding would look like:

    builder.Services.Configure<NavigationManagerOptions>(options =>
    {
        options.PathBaseComparison = StringComparison.OrdinalIgnoreCase;
    });
    

    That would require a new API, e.g.

    + 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:

    app.Map("/dashboard", child => { ... }, new MapOptions 
    { 
        PathBaseComparison = StringComparison.OrdinalIgnoreCase
    });
    

    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.

  17. ThatRickGuy commented on Dec 1, 2025

    @ThatRickGuy

    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.

  18. ilonatommy commented on Dec 2, 2025

    @ilonatommy
    Member

    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:

    builder.Services.AddHttpContextAccessor();
    
    @inject IHttpContextAccessor HttpContextAccessor
    
    @{
        var pathBase = HttpContextAccessor.HttpContext?.Request.PathBase ?? PathString.Empty;
        var baseHref = pathBase.HasValue ? pathBase.ToUriComponent().TrimEnd('/') + "/" : "/";
    }
    ....
    <head>
        <base href="@baseHref" />\
    ....
    

    That would solve the case for blazor web. For standalone webassembly, a workaround like
    #6818 (comment) would still be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Pillar: Technical DebtPriority:3area-blazorfeature-blazor-serverfeature-routing

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions