I am building an Angular 2 app and using .NET Core and Webpack to compile my static resources into a dist folder. I am using a plugin that builds my index.html file and puts it in my wwwroot/dist folder but am struggling on how to configure my Startup.cs file to look for the default file in the dist folder.
Currently I have...
app.UseDefaultFiles();
app.UseStaticFiles();
In my startup.cs file but it will not use the index.html that is generated in my dist folder. Any ideas on what I need to change? The docs do not give much reference from what I could find on how to do this.
Thanks.
As such, the index. html file should always be placed directly within the root directory, not in any sub-folders. Side note: Any folder can have its own index. html file, and a browser directed to that folder with no specific file specified will display index.
By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.
You can use code like below, but it only works when you put empty index.html in the root of wwwroot folder:
app.UseDefaultFiles();
// Serve wwwroot/dist as a root
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\dist"))
});
RequestPath is empty by default so root path will show you index.html or default.html from wwwroot\dist folder.
UPDATE: There is easy and beautiful solution to handle this issue using WebRoot in Program.cs
like below:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist"))
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
In Configure
method of Startup.cs
you just use:
app.UseDefaultFiles();
app.UseStaticFiles();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With