C
C#5mo ago
DeaDo

UseStaticFiles for HTML

I have generated a Static WebApp from my Markdown Files.
c#
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Documentation")),
RequestPath = "/docs",
});
c#
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Documentation")),
RequestPath = "/docs",
});
I want this to serve the File "Documentation/Userguide.html" if the route is "/docs/Userguide" If i serve it via NGNIX i would configure something like this
location / {
try_files $uri $uri.html $uri/ =404;
}
location / {
try_files $uri $uri.html $uri/ =404;
}
14 Replies
DeaDo
DeaDoOP5mo ago
Now im looking for something equivalent in asp.net core the first thing I came up with is a simple middleware:
c#
app.Use(async (context, next) =>
{
await next();
if (context.Request.Path.Value.StartsWith("/docs"))
{
context.Request.Path = context.Request.Path + ".html";
await next();
}
});
c#
app.Use(async (context, next) =>
{
await next();
if (context.Request.Path.Value.StartsWith("/docs"))
{
context.Request.Path = context.Request.Path + ".html";
await next();
}
});
But i don't like it
Angius
Angius5mo ago
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-8.0
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "docs")),
RequestPath = "/docs"
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "docs")),
RequestPath = "/docs"
});
canton7
canton75mo ago
How does that process the .html suffix?
Angius
Angius5mo ago
It just serves all the files wholesale, as they are You request /docs/index.html it serves index.html from docs directory You request /docs/skunga.xyz you get skunga.xyz Just like wwwroot
canton7
canton75mo ago
The question is specifically about how to serve the file Foo.html from the route /docs/Foo OP already has the code you posted in their question. They're asking something else
Angius
Angius5mo ago
Ah, damn, you're right In that case I'd either stick to this custom middleware, or I'd write a controller that does that
canton7
canton75mo ago
It doesn't look like there's anything in PhysicalFileProvider to support this
Angius
Angius5mo ago
[Controller]
[Route("[controller]")]
public class DocsController
{
[HttpGet("{file}")]
public FileResult GetFile(string file)
{
var path = Path.Join("docs", $"{file.ToLower()}.html")
using file = File.Open(path);
return File(file, "text/html")
}
}
[Controller]
[Route("[controller]")]
public class DocsController
{
[HttpGet("{file}")]
public FileResult GetFile(string file)
{
var path = Path.Join("docs", $"{file.ToLower()}.html")
using file = File.Open(path);
return File(file, "text/html")
}
}
Something like this should work
canton7
canton75mo ago
Be very VERY careful there. PhysicalFileProvider has a lot of stuff to prevent you from traversing outside of the webroot. Your code has none of that I suspect I could read any file on the filesystem with that
Angius
Angius5mo ago
Huh, would File.Open() accept a path like docs/../../../../../../../../secretpasswords.html?
canton7
canton75mo ago
Sure
Angius
Angius5mo ago
How do you overcome wanting a .pubkey file with the .html extension being hardcoded into the path?
DeaDo
DeaDoOP5mo ago
Thanks for your help @ZZZZZZZZZZZZZZZZZZZZZZZZZ & @canton7. That try_files feature would be useful in UseStaticFiles because it seems that many SSGs rely on that behavior. Ill stick with the Middleware for now. It works & there are no conflicts (at least for my static files)
Want results from more Discord servers?
Add your server