Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call UseWebRoot in ASP.NET Core 3.0

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.

like image 940
Сергей Avatar asked Oct 11 '19 16:10

Сергей


People also ask

How do I access Wwwroot files?

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.

How do I get wwwroot path in .NET Core?

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 .

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

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.

How do I serve a static file in NET Core?

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).


Video Answer


1 Answers

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\");
        });
like image 186
Kirk Larkin Avatar answered Oct 13 '22 11:10

Kirk Larkin