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

后端 未结 8 536
北荒
北荒 2020-12-30 19:17

How can I get asp.net core to serve an index.html file from inside my wwwroot?

The reason I want to do this is because I an developing an angular 4 app using the ang

相关标签:
8条回答
  • 2020-12-30 19:34
    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

    0 讨论(0)
  • 2020-12-30 19:39

    In the startup.cs inside the configureservices method apply this. Here we create a DefaultFilesOption object and then clear all the defaultfiles set in the path. Next, we add the path of the file we want to set as default. And then we inject the dependency using ' app.UseDefaultFiles(defaultfileoptions). Also, we need to inject static files as dependencies.

    `

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
        DefaultFilesOption defaultFileOptions = new DefaultFilesOption();
        defaultFileOptions.DefaultFileNames.Clear();
        defaultFilesOptions.DefaultFileNames.Add("Index.html");
        app.UseDefaultFiles(defaultFileOptions);
        app.UseStaticFiles();
    }  
    

    `

    0 讨论(0)
  • 2020-12-30 19:41

    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/
        //
        // (Function 1)
        app.UseDefaultFiles(); 
    
    
    
    
        // Enable static files to be served. This would allow html, images, etc. in wwwroot
        // directory to be served.
        //
        // (Function 2)
        app.UseStaticFiles();
    }
    

    Note: The order in which these functions are called is important. In OO programming, it's quite hard not to depend on ordering as objects maintain states that can vary during the lifetime of the object. (You guessed it right, one solution to prevent designs like this is to implement immutability.)

    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

    0 讨论(0)
  • 2020-12-30 19:49

    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.

    0 讨论(0)
  • 2020-12-30 19:50
    return File(System.IO.File.OpenRead(Path.Combine(HostingEnvironment.WebRootPath + "/index.html")), "text/html");
    

    It has to help u

    0 讨论(0)
  • 2020-12-30 19:55

    // By default ASP.net core does not serve the static files such as HTML, css, image etc... I need to configure the middleware in the request processing pipeline for serving static files.

    // This code would be written in Startup.cs class for configuring the middleware components.
    
    
    
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
    
            app.UseDefaultFiles(); // This sets the default page redirection for the in-comming request
                app.UseStaticFiles(); // This serves the static files to the client.
    
            }
    
    0 讨论(0)
提交回复
热议问题