I created a new ASP.NET Core 3.0 Web application and selected the Model-View-Controller option. I wanted to add Blazor Server side, so I added the below to the Startup.cs file.
services.AddServerSideBlazor();
endpoints.MapBlazorHub();
and added the script file
<script src="_framework/blazor.server.js"></script>
in my Layout file.
I created a simple component that displays what I enter in a textbox, and added the component to my Index.cshtml
view. It works within Visual Studio, but when I push it up to my internal 2016 server, the component is rendered but the text is not displayed. The app can't seem to find the blazor.server.js
file.
Is there some other deployment step I am missing to push up the JS file?
Place the JavaScript (JS) tags ( <script>... </script> ) with a script source ( src ) path inside the closing </body> tag after the Blazor script reference. The {webassembly|server} placeholder in the preceding markup is either webassembly for a Blazor WebAssembly app ( blazor.
Blazor is a web framework for building web UI components (Razor components) that can be hosted in different ways. Razor components can run server-side in ASP.NET Core (Blazor Server) versus client-side in the browser on a WebAssembly-based . NET runtime (Blazor WebAssembly, Blazor WASM).
Blazor supports existing JavaScript libraries using IJSRuntime, through JavaScript interop API's.
Blazor lets you build interactive web UIs using C# instead of JavaScript. Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS. Both client and server code is written in C#, allowing you to share code and libraries.
Guidance can be found in the developer tools documentation of each browser maintainer: Perform a manual browser refresh of any webpage of the Blazor app to reload JS files from the server. ASP.NET Core's HTTP Caching Middleware always honors a valid no-cache Cache-Control header sent by a client.
ASP. NET Core This article describes the configuration for serving static files in Blazor apps. For more information on solutions in sections that apply to hosted Blazor WebAssembly apps, see Tooling for ASP.NET Core Blazor. This section applies to Blazor Server apps and the **Server** app of a hosted Blazor WebAssembly solution.
Hi @lxman. The blazor.server.js file is an embedded resource on the Microsoft.AspNetCore.Components.Server.dll assembly, which ships as part of the ASP.NET Core shared framework.
In the Client app's project file ( .csproj) or the standalone Blazor WebAssembly app's project file ( .csproj ): Path to the client app in the Server project of a hosted Blazor WebAssembly solution: /BlazorHostedSample/Server/bin/Release/ {TFM}/publish/wwwroot/app1/
Finally figured it out, I had to add <base href="~/" />
in the head section of the "_Layout.cshtml".
Found the answer here.
In my case,
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "application/octet-stream"
});
is what caused the issue. Replace it with:
app.UseStaticFiles();
solved the problem. I figured maybe DefaultContentType
is where the problem is.
Anyone knows better why that is the case please update this answer.
https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-5.0
Use two calls to UseStaticFiles in Startup.Configure (Startup.cs):
Configure the custom file provider in the first call with StaticFileOptions. The second middleware serves blazor.server.js, which uses the default static files configuration provided by the Blazor framework. C#
using Microsoft.AspNetCore.StaticFiles;
...
var provider = new FileExtensionContentTypeProvider();
provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}";
app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
app.UseStaticFiles();
As blazor.server.js
is a static file, be sure to add app.UseStaticFiles();
in startup.cs
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With