Reputation: 2007
Please consider the following (part of) IApplicationBuilder and IWebHostEnvironment configuration:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "WebContent")),
RequestPath = "/WebContent"
});
In my controller I'm testing 3 different approaches:
return LocalRedirect("~/WebContent/index.html");
This works, but I'm struggling with the next two and wonder why they fail:
return PhysicalFile("~/WebContent/index.html", "text/html");
Throws: FileNotFoundException: Could not find file 'C:\ProjectPath>\bin\Debug\net5.0~\WebContent\index.html'.
return File("~/WebContent/index.html", "text/html");
Throws: FileNotFoundException: Could not find file: ~/WebContent/index.html
How do I access/serve the static files from the latter two?
I'm getting nuts iterating variations of the input, so a little background would be appreciated.
Thanks!
Upvotes: 0
Views: 1432
Reputation: 7200
You can try following code:
private readonly IWebHostEnvironment _hostingEnvironment;
public HomeController(IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public IActionResult Index()
{
var path = Path.Combine(_hostingEnvironment.ContentRootPath, "WebContent/index.html");
return PhysicalFile(path, "text/html");
}
Upvotes: 0