C
C#3d ago
SamPerson

✅ Works in live server, breaks in minimal server. I am a fool.

I'm doing something big-time stupid trying to serve this webpage It works fine with the VSCode extension live server preview, but when I run my custom server, despite (as far as I can tell) serving all the files correctly with the correct mime types, I get a reference error immediately, with the inline JS unable to reference the odin variable defined in odin.js
Uncaught ReferenceError: odin is not defined
at index.html:33:29
Uncaught ReferenceError: odin is not defined
at index.html:33:29
The line in question:
...
<script type="text/javascript" src="odin.js"></script>
<script>
var odinMemoryInterface = new odin.WasmMemoryInterface();
...
...
<script type="text/javascript" src="odin.js"></script>
<script>
var odinMemoryInterface = new odin.WasmMemoryInterface();
...
if i try to access odin.js in my browser directly, it's all there and looks like my server is serving it up fine. The only main Thing i've noticed which might be causing problems, is that if I use HTTP (http://localhost:5134/) vs. HTTPS (https://localhost:7093/), it correctly redirects me to the HTTPS endpoint, but doesn't show the file until i manually refresh the page. So maybe that's related to what's happening here?
5 Replies
SamPerson
SamPersonOP3d ago
It's a super basic asp.net minimal API thing:
public class Program
{
static FileExtensionContentTypeProvider mimeProvider;
static PhysicalFileProvider physicalFileProvider;
public static void Main(string[] args)
{
physicalFileProvider = new PhysicalFileProvider(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
mimeProvider = new();

//
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();

var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();

static string GetFrontendBuildPath(string subpath) { return Path.Join("FrontendBuild", subpath); }
app.MapGet("/test/{subpath}", (HttpContext httpContext, string subpath) =>
{
var fileInfo = physicalFileProvider.GetFileInfo(GetFrontendBuildPath(subpath));
if(mimeProvider.TryGetContentType(fileInfo.PhysicalPath, out string contentType))
{
httpContext.Response.ContentType = contentType;
}
Console.WriteLine($"Getting {fileInfo.PhysicalPath} of type {httpContext.Response.ContentType}");
httpContext.Response.SendFileAsync(fileInfo);
});

app.Run();
}
}
public class Program
{
static FileExtensionContentTypeProvider mimeProvider;
static PhysicalFileProvider physicalFileProvider;
public static void Main(string[] args)
{
physicalFileProvider = new PhysicalFileProvider(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
mimeProvider = new();

//
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();

var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();

static string GetFrontendBuildPath(string subpath) { return Path.Join("FrontendBuild", subpath); }
app.MapGet("/test/{subpath}", (HttpContext httpContext, string subpath) =>
{
var fileInfo = physicalFileProvider.GetFileInfo(GetFrontendBuildPath(subpath));
if(mimeProvider.TryGetContentType(fileInfo.PhysicalPath, out string contentType))
{
httpContext.Response.ContentType = contentType;
}
Console.WriteLine($"Getting {fileInfo.PhysicalPath} of type {httpContext.Response.ContentType}");
httpContext.Response.SendFileAsync(fileInfo);
});

app.Run();
}
}
There's not much surface area for me to be screwing this up, so I'm kind of impressed that I've managed it. Does anyone have a guess for where I'm going wrong? Thank you so much!
k3v
k3v3d ago
Hmmm youre trying to serve static files right? But looks like youre building this yourself in request pipeline. Have you tried using the static file serving apis? https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-10.0
Static files in ASP.NET Core
Learn how to serve and secure static files and configure Map Static Assets endpoint conventions and Static File Middleware in ASP.NET Core web apps.
No description
No description
k3v
k3v3d ago
But if you still want to make current code works, apparently its missing some things - Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) << use this to get the directory properly - app.UseRouting() << to route your request properly - youre not awaiting the SendFileAsync method too - make sure you have this to your csproj too <ItemGroup> <Content Include="FrontendBuild**" CopyToOutputDirectory="Always" /> </ItemGroup>
SamPerson
SamPersonOP3d ago
Appreciate the heads-up! Making it async and awaiting was the trick!
k3v
k3v2d ago
awesome!!! :akshully:

Did you find this page helpful?