简体   繁体   中英

How do I serve an html file in my Web App with ASP.NET without the WWWROOT?

Learning Visual Studios ASP.NET Application how do I attached html file to my project? in startup.cs

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        **app.UseStaticFiles();**
    }
}

My index.html code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Dutch Treat</title>
</head>
<body>
    <h1>Dutch Treat</h1>
</body>
</html>

The instructions say to move into wwwroot but there no wwwroot . Here a pic of what I have

我的代码快照

It seems that you create project using a empty project template, like below.

在此处输入图像描述

It does help create ASP.NET Core project, not ASP.NET project. If you'd like to compare differences between them, you can check this doc:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/choose-aspnet-framework?view=aspnetcore-5.0

How do I serve an html file in my Web App with ASP.NET without the WWWROOT?

As you shared in your code, calling the UseStaticFiles method in Startup.Configure, which enables static files to be served.

But please note that UseStaticFiles default to the file provider pointing at wwwroot , you can manually create the folder with name "wwwroot" and move index.html etc static files to it. Or create a folder with a name (such as "MyStaticFiles" etc) you expect, then configure static file serving from that folder, like below.

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(env.ContentRootPath, "MyStaticFiles"))
});

wwwroot is nothing but a folder within the project. If it does not exists, you could just add a folder named wwwroot directly under your project and then add the html file under it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM