受欢迎的博客标签

How to Change the default wwwroot to the specified directory in ASP.NET Core(iaspnetcore)

Published

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.

In the standard ASP.NET Core application, static files can be served from the wwwroot folder of an application or any other folder under it. Now, only those files that are in the web root - wwwroot folder can be served over an http request. All other files are blocked and cannot be served by default.

Generally, there should be separate folders for the different types of static files such as JavaScript, CSS, Images, library scripts etc. in the wwwroot folder as shown below image.

 

you can access static files with base URL and file name. For example, we can access above site.css file in the css folder by

http://localhost:<port>/css/app.css

Remember, you need to include a middleware for serving static files in the Configure method of Startup.cs.

You can Change the default wwwroot to the specified directory in ASP.NET Core,set it as a web root while preparing hosting environment in the program.cs.

step 1. Create New Folder

\src\Presentation\Nop.Web\wwwroot\Themes\RootTheme

Step 2. Configure New Folder as root

For example, let's Change the default wwwroot folder to RootTheme folder. Now, call UseStaticFiles() method to configure RootTheme folder as a web root folder in Startup.cs class as shown below.

 var themes = "Bootstrap4";
            //Change the default wwwroot to the specified directory so that the themes css and js can be designed independently

            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "Themes", "RootTheme")),
              
                RequestPath = new PathString("")
            });

Thus, you can rename the default web root folder wwwroot as per your choice.

Step 3:test

*.css

visit:

http//localhost:5000/content/bootstrap/bootstrap.min.css

Send file   from path:

src\Presentation\Nop.Web\wwwroot\Themes\RootTheme\Content\bootstrap\bootstrap.min.css

 

favicon.ico

vist 

http://www.iaspnetcore.com/favicon.ico

output:

Send file from path:src\Presentation\Nop.Web\wwwroot\favicon.ico

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost:5000/favicon.ico - -
 info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/favicon.ico'. 
Physical path: '/src/Presentation/Nop.Web/bin/release/netx.0/publish/wwwroot/favicon.ico'

search.xml

http://www.iaspnetcore.com/search.xml

output

Send file from path:src\Presentation\Nop.Web\wwwroot\search.xml

image *.png

https://www.iaspnetcore.com/Content/images/logo.png

output

Sending file. Request path: '/Content/images/logo.png'. Physical path: '/src/Presentation/Nop.Web/bin/release/net6.0/publish/wwwroot/Themes/RootTheme/Content/images/logo.png

useful links:

docs.microsoft.com Static files in ASP.NET Core 5.x

https://github.com/dotnet/aspnetcore/blob/c925f99cddac0df90ed0bc4a07ecda6b054a0b02/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs