Setting index.html as default page in asp.net core

别来无恙 提交于 2019-12-03 04:22:53

Just use this in startup.cs:

app.UseFileServer();

It's shorthand for:

app.UseDefaultFiles();
app.UseStaticFiles();

... and it avoids issues with having to have those in the correct order (as shown above)

I needed to declare UseDefaultFiles() before UseStaticFiles().

app.UseDefaultFiles();
app.UseStaticFiles();

Install the NuGet package Microsoft.AspNetCore.StaticFiles.

Now, in Startup.Configure method, add:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Serve the files Default.htm, default.html, Index.htm, Index.html
    // by default (in that order), i.e., without having to explicitly qualify the URL.
    // For example, if your endpoint is http://localhost:3012/ and wwwroot directory
    // has Index.html, then Index.html will be served when someone hits
    // http://localhost:3012/
    app.UseDefaultFiles(); 

    // Enable static files to be served. This would allow html, images, etc. in wwwroot
    // directory to be served. 
    app.UseStaticFiles();
}

You should now get files served from wwwroot directory (use UseWebRoot if you want to change it to something else).

Source: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

app.UseDefaultFiles(new DefaultFilesOptions {
    DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles();

This is optimal since the UseDefaultFiles URL rewriter will only search for index.html, and not for the legacy files: default.htm, default.html, and index.htm.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document

If you want to use another document like foo.html for example as your default document, you can do so using the following code in Startup.Configure method.

// Specify foo.html as the default document
DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
defaultFilesOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("foo.html");
// Add Default Files Middleware
app.UseDefaultFiles(defaultFilesOptions);
// Add Static Files Middleware
app.UseStaticFiles();

Approach 2:

UseFileServer combines the functionality of UseStaticFiles, UseDefaultFiles and UseDirectoryBrowser middleware. DirectoryBrowser middleware, enables directory browsing and allows users to see files within a specified directory. We could replace UseStaticFiles and UseDefaultFiles middlewares with UseFileServer Middleware.

// Use UseFileServer instead of UseDefaultFiles & UseStaticFiles

FileServerOptions fileServerOptions = new FileServerOptions();
fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear();
fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add("foo.html");
app.UseFileServer(fileServerOptions);
satty

You are mixing both MVC and Default files serving (useDefaultFiles). Comment out the below lines from your code

app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}");
    });

and use only app.UseDefaultFiles();. It will start working.

return File(System.IO.File.OpenRead(Path.Combine(HostingEnvironment.WebRootPath + "/index.html")), "text/html");

It has to help u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!