[Blazor Hosted .NET 6] Server is no more returning index.html

Hi, I've an application I'm developing for I while. Today, after some refactoring, I have that even though application starts correctly, I can e.g. perform REST request to controllers, if I navigate to the endpoint (http://localhost:5254), a 404 is returned. In the server debug log I found this:
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://192.168.0.51:5254/ - -
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
trce: Microsoft.AspNetCore.ResponseCompression.ResponseCompressionProvider[3]
This request accepts compression.
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[4]
The request path / does not match a supported file type
dbug: Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1001]
1 candidate(s) found for the request path '/'
dbug: Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1005]
Endpoint 'Fallback {*path:nonfile}' with route pattern '{*path:nonfile}' is valid for the request path '/'
dbug: Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware[1]
Request matched endpoint 'Fallback {*path:nonfile}'
dbug: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[9]
AuthenticationScheme: Bearer was not authenticated.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'Fallback {*path:nonfile}'
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[5]
The request path /index.html does not match an existing file
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'Fallback {*path:nonfile}'
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[9]
Connection id "0HMKCN544RENL" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 GET http://192.168.0.51:5254/ - - - 404 0 - 68.5228ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://192.168.0.51:5254/ - -
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
trce: Microsoft.AspNetCore.ResponseCompression.ResponseCompressionProvider[3]
This request accepts compression.
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[4]
The request path / does not match a supported file type
dbug: Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1001]
1 candidate(s) found for the request path '/'
dbug: Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1005]
Endpoint 'Fallback {*path:nonfile}' with route pattern '{*path:nonfile}' is valid for the request path '/'
dbug: Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware[1]
Request matched endpoint 'Fallback {*path:nonfile}'
dbug: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[9]
AuthenticationScheme: Bearer was not authenticated.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'Fallback {*path:nonfile}'
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[5]
The request path /index.html does not match an existing file
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'Fallback {*path:nonfile}'
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[9]
Connection id "0HMKCN544RENL" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 GET http://192.168.0.51:5254/ - - - 404 0 - 68.5228ms
It seems like even though /index.html is under wwwroot, it is not published.
1 Reply
alkasel#159
alkasel#1592y ago
The Server Program.cs is more or less this:
public static void Main(string[] args) {
_builder = WebApplication.CreateBuilder(args);
_mvcBuilder = _builder.Services.AddControllersWithViews();
_builder.Services.AddHttpContextAccessor();
_builder.Services.AddRazorPages();

// ... Custom services are added ...

// ... Authentication and Authorization services are added ...

_builder.Services.AddSignalR();
_builder.Services.AddResponseCompression(opts => {
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});

List<string> endpoints = _builder.Configuration.GetSection("ListeningEndpoints").Get<List<string>>();
_builder.WebHost.UseUrls(endpoints.ToArray());
foreach (string endpoint in endpoints) {
_log.Info(string.Format("Application listening on {0}", endpoint));
}

WebApplication app = _builder.Build();

app.UseResponseCompression();
if (app.Environment.IsDevelopment()) {
app.UseWebAssemblyDebugging();
} else {
app.UseExceptionHandler("/Error");
}
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapHub<ChatHub>("/chathub");
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
}
public static void Main(string[] args) {
_builder = WebApplication.CreateBuilder(args);
_mvcBuilder = _builder.Services.AddControllersWithViews();
_builder.Services.AddHttpContextAccessor();
_builder.Services.AddRazorPages();

// ... Custom services are added ...

// ... Authentication and Authorization services are added ...

_builder.Services.AddSignalR();
_builder.Services.AddResponseCompression(opts => {
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});

List<string> endpoints = _builder.Configuration.GetSection("ListeningEndpoints").Get<List<string>>();
_builder.WebHost.UseUrls(endpoints.ToArray());
foreach (string endpoint in endpoints) {
_log.Info(string.Format("Application listening on {0}", endpoint));
}

WebApplication app = _builder.Build();

app.UseResponseCompression();
if (app.Environment.IsDevelopment()) {
app.UseWebAssemblyDebugging();
} else {
app.UseExceptionHandler("/Error");
}
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapHub<ChatHub>("/chathub");
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
}
I've checked git for changes, but I see nothing particularly terrible. Thus I was wondering it someone could help be reconstruct the procedure .NET performs in order to publish wwwroot folder, so I can try to understand what's wrong with it I've fond the problem: while cleaning the solution, I removed a reference to client from server. Now I restored it and index.html is retrieved correctly! Even so, all other files under wwwroot (css, .js...) were still not found. Turned out problem was I removed app.UseStaticFiles() to do some experiment of index.html file mapping. Now everything is restored!