Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core changing the location where my default index.html file is

Tags:

asp.net-core

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.

like image 388
dev53 Avatar asked Jul 26 '17 02:07

dev53


People also ask

Does index HTML have to be in root folder?

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.

Why do we have wwwroot folder?

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.


1 Answers

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();
like image 198
user2771704 Avatar answered Oct 16 '22 14:10

user2771704