Jacek Majer
Jacek Majer

Reputation: 385

How to combine path with WebRootPath

I need to upload some files to server using my app and I need to deliver a full path of the folder where I want to save files. I am using Path.Combine method with webHostEnvironment.WebRootPath additional folders path from appsettings.json and file name. The code looks like this:

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

And the problem is that the webHostEnvironment.WebRootPath is just ommited in the path variable (path looks like pathString + picture.Name).

Any idea how to get proper path?

Upvotes: 5

Views: 28386

Answers (3)

SUNIL DHAPPADHULE
SUNIL DHAPPADHULE

Reputation: 2871

If you are using Asp.net core 3 then use IWebHostEnvironment instead of IHostingEnvironment. Inject IWebHostEnvironment as a dependency into the dependent class.https://github.com/aspnet/AspNetCore/issues/7749

public class HomeController : Controller
{
    private readonly IWebHostEnvironment _webHostEnvironment;

    public HomeController(IWebHostEnvironment webHostEnvironment)
    {
        _webHostEnvironment= webHostEnvironment;
    }

    public IActionResult Index()
    {
        return Content(_webHostEnvironment.WebRootPath + "\n" + _webHostEnvironment.ContentRootPath);
    }
}

When Microsoft.Extensions.Hosting was introduced in 2.1 some types like IHostingEnvironment and IApplicationLifetime were copied from Microsoft.AspNetCore.Hosting. Some 3.0 changes cause apps to include both the Microsoft.Extensions.Hosting and Microsoft.AspNetCore.Hosting namespaces. Any use of those duplicate types causes an "ambiguous reference" compiler error when both namespaces are referenced.

This error has been addressed in 3.0.0-preview3 by marking the following types obsolete and replacing them with new types. There have not been any behavioral changes made for the new types, only naming.

Obsolete types (warning):

Microsoft.Extensions.Hosting.IHostingEnvironment
Microsoft.AspNetCore.Hosting.IHostingEnvironment
Microsoft.Extensions.Hosting.IApplicationLifetime
Microsoft.AspNetCore.Hosting.IApplicationLifetime
Microsoft.Extensions.Hosting.EnvironmentName
Microsoft.AspNetCore.Hosting.EnvironmentName

New types:

Microsoft.Extensions.Hosting.IHostEnvironment
Microsoft.AspNetCore.Hosting.IWebHostEnvironment : IHostEnvironment
Microsoft.Extensions.Hosting.IHostApplicationLifetime
Microsoft.Extensions.Hosting.Environments

Note the new IHostEnvironment IsDevelopment, IsProduction, etc. extension methods are in the Microsoft.Extensions.Hosting namespace which may need to be added to your app.

For 3.0 both the old and new types will be available from HostBulder's and WebHostBuilder's dependency injection containers. The old types will be removed in 4.0.

Upvotes: 8

Xueli Chen
Xueli Chen

Reputation: 12725

In ASP.NET Core, the physical paths to both the web root and the content root directories can be retrieved by injecting and querying the IHostingEnvironment service:

 public class HomeController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public ActionResult Post(Picture picture)
    {
        var pathString = configuration.GetValue<string>("UploadPaths:Pictures");
        var path = Path.Combine(_hostingEnvironment.WebRootPath, pathString, picture.Name);
        return Content(path);
    }
}

You could take a look at Getting the Web Root Path and the Content Root Path in ASP.NET Core.

Upvotes: 0

corranrogue9
corranrogue9

Reputation: 206

You should checkout the full documentation for Path.Combine. There are some weird things that can happen. Based on what you are describing, it seems like pathString coming from your configuration file has a root in it (i.e. it is not a relative path).

var path = Path.Combine("path1", "c:\path2", "path3");
// path is c:\path2\path3, since an argument with a root overrides any previous argument

Upvotes: 1

Related Questions