Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

blazor.server.js file not found

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?

like image 366
Mike Casas Avatar asked Sep 24 '19 21:09

Mike Casas


People also ask

Where do I put JavaScript in Blazor?

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.

What is Blazor server JS for?

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

Can you use JavaScript libraries in Blazor?

Blazor supports existing JavaScript libraries using IJSRuntime, through JavaScript interop API's.

Does Blazor compile to JavaScript?

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.

How do I reload JS files from the Blazor server?

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.

Can I serve static files in Blazor apps?

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.

What is Blazor server DLL?

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.

Where can I find the Blazor WebAssembly client app path?

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/


4 Answers

Finally figured it out, I had to add <base href="~/" /> in the head section of the "_Layout.cshtml".

Found the answer here.

like image 165
Mike Casas Avatar answered Oct 16 '22 07:10

Mike Casas


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.

like image 29
Tom Charles Zhang Avatar answered Oct 16 '22 07:10

Tom Charles Zhang


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();
like image 4
erymuzuan Avatar answered Oct 16 '22 07:10

erymuzuan


As blazor.server.js is a static file, be sure to add app.UseStaticFiles(); in startup.cs.

like image 1
E13 Avatar answered Oct 16 '22 05:10

E13