Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wwwroot folder in asp.net core 2.2

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.

like image 288
FranB Avatar asked Mar 03 '19 15:03

FranB


People also ask

What is the Wwwroot folder in ASP.NET Core?

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.

Where can I find wwwroot folder?

Right-click the Web application you want more information about, such as SharePoint (80), and then click Properties. In the Default Web Site Properties window, click the Home Directory tab. The Local Path field in this tab shows the Web application root folder.

How can add wwwroot folder in ASP.NET Core API?

In order to add the wwwroot folder, right-click on the project and then select add => new folder option and then provide the folder name as wwwroot.

What should be in the wwwroot folder?

The wwwroot folder is new in ASP.NET 5 to store all of the static files in your project. Any files including HTML files, CSS files, image files, and JavaScript files which are sent to the user's browser should be stored inside this folder.


3 Answers

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.

like image 127
SUNIL DHAPPADHULE Avatar answered Oct 05 '22 23:10

SUNIL DHAPPADHULE


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>();
}
like image 27
Shriram Navaratnalingam Avatar answered Oct 05 '22 23:10

Shriram Navaratnalingam


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

like image 32
Aref Zamani Avatar answered Oct 06 '22 00:10

Aref Zamani