In ASP.NET Core 2.2, I could set UseWebRoot()
like:
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.UseUrls("http://*:5000")
.UseWebRoot(@".\WebSite\wwwroot\")
.Build()
.Run();
}
But I do not know how I should do it today because there is no such method anymore.
You can access static files with base URL and file name. For example, we can access above app. 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.
The path of the wwwroot folder is accessed using the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core. The IHostingEnvironment is an interface for .
Adding wwwroot (webroot) folder in ASP.NET Core: 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.
To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).
ASP.NET Core 3.0 projects use the Generic Host, by default. In the project templates, it's configured like this:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseStartup<Startup>();
});
In the example above, webHostBuilder
is an implementation of IWebHostBuilder
, which still contains the UseWebRoot
extension method. That means you can call it as you did for 2.2, but it's just moved to inside the delegate passed in to ConfigureWebHostDefaults
shown above. Here's the complete example:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseStartup<Startup>();
webHostBuilder.UseWebRoot(@".\WebSite\wwwroot\");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With