Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Server Project with Blazor running in different path #12

Open
neo302 opened this issue Oct 9, 2022 · 5 comments
Open

Blazor Server Project with Blazor running in different path #12

neo302 opened this issue Oct 9, 2022 · 5 comments

Comments

@neo302
Copy link

neo302 commented Oct 9, 2022

Hello and thank you for the project!
I have a project running with blazor in separate path - https://localhost:44368/blazor/
Therefore I have:
Startup.cs
endpoints.MapBlazorHub("/Blazor/_blazor");
endpoints.MapFallbackToPage("~/Blazor/{*clientroutes:nonfile}", "/Blazor/_Host");

Pages/Blazor/_Host.cshtml
@page "/blazor"
...
<head>
...
<link rel="stylesheet" href="/css/site.css" />
<base href="~/blazor/" />
</head>
<body>
...
<script src="~/_framework/blazor.server.js"></script>
</body>

All of my other Blazor content works fine.
When attempting to use this project, I receive the error:
{"Failed to fetch dynamically imported module: https://localhost:44368/blazor/_content/Toolbelt.Blazor.SpeechSynthesis/script.module.min.js?v=10.0.0\nTypeError:
Failed to fetch dynamically imported module: https://localhost:44368/blazor/_content/Toolbelt.Blazor.SpeechSynthesis/script.module.min.js?v=10.0.0"}

Do you know how I can get this issue resolved?

Thank you!

@jsakamoto
Copy link
Owner

That trouble you reported may not be a matter of this SpeechSynthesis library. If you want to place your Blazor App to a sub-path (not root path), please follow the official document below.

As the document above says, you don't have to change calling the MapBlazorHub() and MapFallbackToPage(), and change the _Host.cshtml. What you should do is only add calling the UsePathBase() method. (and you may have to change the launchSettings.json a bit.)

For your reference, I'll attach a Blazor Server .NET6.0 sample app project that will work on the URL http://localhost:.../foo/bar.

📦BlazorServerApp1.zip

The zip file I attached above includes the Git repository, so you can see the commit history and diff to place the app on the sub-path.

@neo302
Copy link
Author

neo302 commented Oct 11, 2022

Hello and thank you for the response! With my use case, the Blazor app is in a folder within an existing MVC project (a single project) as I currently need my Blazor app to work this way. Correct, I don't think that there is an issue with the SpeechSynthesis library. I believe the issue in my use case is the SpeechSynthesis library is putting the js at https://localhost:44368/_content/... and looking at this location. With how I need to configure my project, when this library is integrated, it is looking at https://localhost:44368/**blazor**/_content/..., but that is not where the js is. It would be great to be able to configure SpeechSynthesis to put the js at https://localhost:44368/**blazor**/_content/. I apologize as I should have added more detail around my use case.

@jsakamoto
Copy link
Owner

@neo302
Sorry too late.
The location of the JavaScript file bundled with any NuGet package is placed under the "~/_content/..." basically because it is determined by the ASP.NET Core's static Web assets feature.
I could make the location of the JavaScript file of the SpeechSynthesis NuGet package changeable, but it must not be an appropriate solution for you because not only the SpeechSynthesis package but also any other NuGet packages will not provide such a customization ability.
(Please imagine when you add another Blazor component NuGet package which includes JavaScript files to your project.)

I'll keep investigating this scenario for you, but please understand this is a general problem on Blazor application programming. And it might take much longer than you expected because I'm currently so busy caring for other issues with my products. It might be better to ask about this problem on other online forums such as StackOverflow.com, Reddit, etc.

P.S.
Could you provide me a minimal sample project that can reproduce your problem? Currently, I have to spend a lot of time creating a project that can reproduce this problem before getting started with the investigation.

@jsakamoto
Copy link
Owner

jsakamoto commented Oct 16, 2022

Hi @neo302,

I've tried to reproduce your case and resolve it.
I'll recommend trying to use the "ASP.NET Core URL Rewriting Middleware". For more detail, please follow the next link.

As I already said, it is hard that change the location of static contents bundled with Razor class libraries or NuGet packages from the URL "/_content/..." to the other URL due to the design of the Static Web Assets feature in the "ASP.NET Core".

Instead, we can rewrite the request URL to the other URL where the requested static files exist exactly by using that "ASP.NET Core URL Rewrite middleware". For example, we can rewrite the request URL "/blazor/_content/foo/bar.js" to "/_content/foo/bar.js" in the early stage of the request processing pipeline, then the ASP.NET Core server will be able to respond to the contents of "bar.js" against to the request "/blazor/_content/foo/bar.js" from a web browser.

Please see also the real example project I attached to this post below.

📦BlazorServerUnderMVC.zip

image

I hope that the example project and this information be helpful for you.

@neo302
Copy link
Author

neo302 commented Oct 16, 2022

@jsakamoto Sorry for the delay. Thank you for taking time to look at this and for providing this solution! I think this is probably a nice approach to handling it and should work well. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants