dotnet / dotnet/aspnetcore

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

Open
#43,191 19 comments 5 reactions 0 assignees View on GitHub
area-blazor feature-blazor-server feature-observability Pillar: Technical Debt Priority:1
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] 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](https://github.com/dotnet/aspnetcore/issues/14246)
[#24631](https://github.com/dotnet/aspnetcore/issues/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
```c#
@(await Html.RenderComponentAsync()
```
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 `` 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:

```js

Blazor.start({
configureSignalR: function (builder) {
builder.withUrl("/_blazor", {

});
}
});

```

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

```c#
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.