I am trying to return html file from my ASP NET 7 app. In that html files i am calling couple of scripts. Html file and scripts exist in same root directory:
using Microsoft.Net.Http.Headers;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// middleware
app.Use(async (context, next) =>
{
var html = await File.ReadAllTextAsync("index.html");
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(html);
await next();
});
app.Run();
The problem is, that scripts are loaded with Content-Type: text/html instead of application/javascript:
My question is, how to call only .js file extensions with diffrent content type using ASP .NET?

index.htmlfor every request, even for the scripts. You should look into using theStaticFileMiddlewareinstead which will handle this for you.