Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we set ContentRootPath and WebRootPath?

Tags:

asp.net-core

We're ending up with the following ContentRoot and WebRoot when we run our app from IIS.

ContentRoot:  C:\MyApp\wwwroot
WebRoot:      C:\MyApp\wwwroot\wwwroot

Here is how we are setting ContentRoot and WebRoot.

public class Startup
{
    private readonly IHostingEnvironment _hostingEnv;

    public Startup(IHostingEnvironment hostingEnv)
    {
        _hostingEnv = hostingEnv;
    }

    public void Configure(IApplicationBuilder app)
    {
        app.Run(context =>
        {
            // test output
            context.Response.WriteAsync(_hostingEnv.ContentRootPath + "\r\n");
            return context.Response.WriteAsync(_hostingEnv.WebRootPath + "\r\n");
        });
    }

    public static void Main(string[] args)
    {
        var contentRoot = Directory.GetCurrentDirectory();
        var webRoot = Path.Combine(contentRoot, "wwwroot");

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseIISPlatformHandlerUrl()
            .UseContentRoot(contentRoot)  // set content root
            .UseWebRoot(webRoot)          // set web root
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

From intellisense I see that...

  • ContentRootPath contains the application content files.
  • WebRootPath contains the web-servable content files.

How do we make the test output look instead like this:

ContentRoot:  C:\MyApp\
WebRoot:      C:\MyApp\wwwroot\
like image 868
Shaun Luttin Avatar asked Apr 14 '16 22:04

Shaun Luttin


People also ask

What is Contentrootpath and WebRootPath?

The content root path is the absolute path to the directory that contains the application content files. The web root path is the absolute path to the directory that contains the web-servable application content files.

How do I get WebRootPath in .NET Core?

If you need to get the webroot path in Web API in ASP.Net Core. You can utilize the IWebHostEnviroment interface by inheriting it in your Web API class controller.

How do I merge paths with WebRootPath?

var pathString = configuration. GetValue<string>("UploadPaths:Pictures"); var path = Path. Combine(webHostEnvironment. WebRootPath, pathString, picture.Name);

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


1 Answers

While RC2 documentation is still being prepared, here is what I learned while trying to deploy pre-RC2 app as Azure Web App:

  1. There is no Visual Studio tooling yet, so the app must be published and deployed manually over FTP. For publishing, use: dotnet publish --configuration Release --output ./approot

  2. If connected to Azure over FTP, you will probably see something similar to:

enter image description here

  1. The "approot" folder can be replaced with the published one (the web.config is left in the approot).

  2. The "approot" must be configured as a virtual application in Azure Portal (the default was site\wwwroot):

enter image description here

  1. An the last thing to get static files served from the wwwroot folder, the Startup.cs file should be modified to include custom UseWebRoot call:
var currentDirectory = Directory.GetCurrentDirectory();

var host = new WebHostBuilder()
    .UseKestrel()
    .UseWebRoot(Path.Combine(currentDirectory, "..", "wwwroot"))
    .UseDefaultHostingConfiguration(args)
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

After these steps you should have ASPNET Core pre-RC2 web app running on Azure.

like image 76
Zygimantas Avatar answered Sep 19 '22 23:09

Zygimantas