简体   繁体   English

如何将路径与WebRootPath结合

[英]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. 我正在将Path.Combine和文件名中的Path.Combine方法与webHostEnvironment.WebRootPath其他文件夹路径一起使用。 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). 问题是, webHostEnvironment.WebRootPath只是在path变量中被忽略了(path看起来像pathString + picture.Name)。

Any idea how to get proper path? 任何想法如何获得正确的道路?

You should checkout the full documentation for Path.Combine . 您应该查看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). 根据您的描述,似乎来自配置文件的pathString包含一个根(即,它不是相对路径)。

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: 在ASP.NET Core中,可以通过注入和查询IHostingEnvironment服务来检索到Web根目录和内容根目录的物理路径:

 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 . 您可以看看在ASP.NET Core获取Web根路径和内容根路径

If you are using Asp.net core 3 then use IWebHostEnvironment instead of IHostingEnvironment . 如果您使用的是Asp.net core 3,请使用IWebHostEnvironment而不是IHostingEnvironment Inject IWebHostEnvironment as a dependency into the dependent class. 将IWebHostEnvironment作为依赖项注入到依赖类中。 https://github.com/aspnet/AspNetCore/issues/7749 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. 在2.1中引入Microsoft.Extensions.Hosting时,从Microsoft.AspNetCore.Hosting复制了一些类型,如IHostingEnvironment和IApplicationLifetime。 Some 3.0 changes cause apps to include both the Microsoft.Extensions.Hosting and Microsoft.AspNetCore.Hosting namespaces. 某些3.0更改导致应用程序同时包含Microsoft.Extensions.Hosting和Microsoft.AspNetCore.Hosting命名空间。 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. 通过将以下类型标记为过时并将其替换为新类型,可以解决此错误(在3.0.0-preview3中)。 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. 请注意,新的IHostEnvironment IsDevelopment,IsProduction等。扩展方法位于Microsoft.Extensions.Hosting命名空间中,可能需要将其添加到您的应用程序中。

For 3.0 both the old and new types will be available from HostBulder's and WebHostBuilder's dependency injection containers. 对于3.0,新旧类型都可以从HostBulder和WebHostBuilder的依赖项注入容器中获得。 The old types will be removed in 4.0. 旧类型将在4.0中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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