Skip to content

<base href="/" /> or base-tag alternative for Blazor MVC integration. #43191

Open
@Varin6

Description

@Varin6

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Issue related to those issues, yet I think since those were reported, this is still a problem for a lot of programmers.

#14246
#24631

I am integrating a few components into an existing, large, multitenant MVC application - .NET6.
Applications frontend is views/partial views, I am injecting the Blazor components using

@(await Html.RenderComponentAsync<MyComponent>()

and everything works fine as long as it's on the main page. As soon as I try adding components anywhere else, the _blazor/initializers can't be found as blazor.js tries to look for them in the current path rather than the base path of the app.

Adding <base href="/" /> fixes this issue, but then it ruins a lot of other things in the existing MVC app, including any anchor tabs/bootstrap tabs, if the blazor component exists on that view.

The choice I have right now is to either find an alternative for a base-tag or somehow fix the issues caused by the base-tag in the massive MVC app. From a business perspective, we wouldn't want to put so much work because base-tag is mandatory and there are no other ways of making it work. I feel like the integration side of Blazor is bit neglected, and businesses often first try new technology they want to implement before they decide to write an app fully using it. This stops the propagation of this tech.

Describe the solution you'd like

Currently I solved it (so far no errors in the console and everything works fine) with those two additions to my code:

In _Layout.cshtml I disable the autostart and add this config:

<script src="~/_framework/blazor.server.js" autostart="false"></script>
<script>
Blazor.start({
       configureSignalR: function (builder) {
            builder.withUrl("/_blazor", {
                
           });
        }
    });
</script>

This doesn't seems to solve the _blazor/initializers path so in Program.cs I had to add a rewrite:

var rewriteOptions = new RewriteOptions();
    rewriteOptions.AddRewrite("_blazor/initializers", "/_blazor/initializers", skipRemainingRules: true);
    app.UseRewriter(rewriteOptions);

This works so far, not sure if I will need to add more rewrites when some other errors appear, I can't seem to find any more information about how to solve it, while finding a lot of topics about this same issue I am currently having, all related to integration of Blazor into existing MVC projects.

It could simply be just an elegant solution of passing one parameter as an option, for those cases where base-tag is not desired.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions