简体   繁体   中英

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?

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 (ie 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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM