wwwroot folder in asp.net core 2.2
Asked Answered
I

4

51

I have just created a new ASP.NET Core web application and selected an empty project template. I am using Visual Studio Community 15.7.1.

Upon a first look at the solution explorer I can see no "wwwroot" folder there. Does anyone happen to know if with asp.net core 2.2 the wwwroot is not visible any longer?

I checked the documentation and I could not find anything. No issues in creating another folder and serve my static files from there but I was just curious.

Thanks in advance.

Infringe answered 3/3, 2019 at 15:27 Comment(3)
By design empty project template would not add wwwroot folder, controllers folders, models folder and so on.Frederik
Thanks Tanvir, it does if I select asp.net core 2.0Infringe
It really shouldn't. Only the ASP.NET Core MVC tempaltes should have itSabba
L
51

Try adding a new folder to the project and call it wwwroot. It should take on the appearance with the proper icon and work as expected. This always happens to me with .NET Core web apps when I use the empty project template. This is occurring in VS 2017 v15.9.3.

Lymphoblast answered 15/5, 2019 at 10:14 Comment(0)
W
32

After manually creating the wwwroot folder, we need to add the staticFile middle-ware in the Configure() method of Startup file, as shown below inorder to serve the static file.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        //Adding static file middleware
        app.UseStaticFiles();

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }

We can also rename the default wwwroot folder as we want for an example, if we want to rename it as content

we need to call UseWebRoot() method to configure Content folder as a web root folder in the Main() method of Program class as shown below.

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseWebRoot("Content")
            .UseStartup<Startup>();
}
Winona answered 21/8, 2019 at 4:27 Comment(3)
Upvoted this answer. But added a caveat in my answer about adding a file to the wwwroot folder.Delbert
What about security is this way safe?Tai
Why is this different in .NET core 2.1? In 2.1 and below, in an empty project all of this is already done for you (wwwroot folder is automatically created, etc). Why did they remove this from .NET Core Web Applications in version 2.2 and above?Adrastus
P
19

you must add folder manually and then rename it to wwwwroot. vs detect it as launch folder

Powerdive answered 15/5, 2019 at 8:14 Comment(3)
lunch folders! Yum!Homosporous
Can directly add folder with by name wwwrootTompion
did you mean launch folderAcquire
D
3

For future readers:

A. Read this ! https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1 (Or read shiram's excellent answer which briefly sums it up!)

B. (More practical). You need to put some kind of file inside the "wwwroot" folder. And mark it as "Copy Always" or "Copy If Newer".

Example: (typical "rest" aspnetcore project)

bin
Controllers
Controllers\ValuesController.cs
obj
Program.cs
Startup.cs
MyCompany.MyTechnology.MyProject.csproj
MyCompany.MyTechnology.MyProject.csproj.user
wwwroot
wwwwroot\StaticContentExampleOne.txt

(Mark "StaticContentExampleOne.txt" as "Copy Always" or "Copy If Newer")

Now you should get the wwwroot folder

example:

"bin\debug\netcoreapp3.1\wwwroot"

and

"bin\debug\netcoreapp3.1\wwwroot\StaticContentExampleOne.txt"

I don't there is a difference between 2.x and 3.x in this scenario.

Delbert answered 23/1, 2020 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.