Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试将图像保存到wwwroot时发生DirectoryNotFoundException_C#_Asp.net Core Mvc - Fatal编程技术网

C# 尝试将图像保存到wwwroot时发生DirectoryNotFoundException

C# 尝试将图像保存到wwwroot时发生DirectoryNotFoundException,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我用这个代码来保存一个文件 [HttpPost] public IActionResult Upload(string office, IFormFile file) { if (file.Length > 0) { var filePath = Path.GetFullPath(Path.Combine(_environment.WebRootPath, _configuration["SydneyFloorplanPath"]));

我用这个代码来保存一个文件

[HttpPost]
public IActionResult Upload(string office, IFormFile file)
{
    if (file.Length > 0) {
        var filePath = Path.GetFullPath(Path.Combine(_environment.WebRootPath, 
            _configuration["SydneyFloorplanPath"]));

        using (var fileStream = new FileStream(filePath, FileMode.Create)) {
            file.CopyTo(fileStream);
        }
    }
    return RedirectToAction("Index", new{office = office});
}
但在实例化
FileStream
时,我遇到了一个非常恼人的问题:

System.Private.CoreLib.dll中发生类型为“System.IO.DirectoryNotFoundException”的异常,但未在用户代码中处理:“找不到路径“C:\images\Floorplan\sydney.pdf”的一部分。”

但这不是我需要的道路。
Images
文件夹位于我的项目中的
wwwroot\Images

如果
WebRootPath
实际上没有给我一个可用的相对路径,那么它有什么意义

请注意,
\u环境
是一个注入的
IHostingEnvironment

我还注意到如果我打电话

var two = _environment.WebRootPath;
变量包含完整的路径<代码>“C:\\Users\\bassie\\source\\repos\\TFS\\dstools\\wwwroot”

但是在调用
Path.Combine
之后,我突然有了
“C:\\images\\Floorplan\\sydney.pdf”
。。。为什么?

编辑:

_环境是一个
IHostingEnvironment
,例如:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCorePathMapping
{
    public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;

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

        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;

            return Content(webRootPath + "\n" + contentRootPath);
        }
    }
}

我设法使这项工作顺利进行

[HttpPost]
public IActionResult Upload(string office, IFormFile file)
{
    var webRootPath  = _environment.WebRootPath;
    var floorPlanPath =  _configuration["SydneyFloorplanPath"];

    if (file.Length > 0) {
        var filePath1 = Path.Combine(floorPlanPath,webRootPath.ReplaceFirst("/", ""));

        using (var fileStream = new FileStream(filePath1, FileMode.Create)) {
            file.CopyTo(fileStream);
        }
    }
    return RedirectToAction("Index", new{office = office});
}
其中
ReplaceFirst
是一个简单的
StringExtension

public static string ReplaceFirst(this string text, string search, string replace)
{
    int pos = text.IndexOf(search);
    if (pos < 0)
    {
        return text;
    }
    return $"{text.Substring(0, pos)}{replace}{text.Substring(pos + search.Length)}";
}
publicstaticstringreplacefirst(此字符串文本、字符串搜索、字符串替换)
{
int pos=text.IndexOf(搜索);
如果(位置<0)
{
返回文本;
}
返回$“{text.Substring(0,pos)}{replace}{text.Substring(pos+search.Length)}”;
}

因此,在
webRootPath
中,似乎领先的
/
打破了我的
路径。Combine
call

什么
\u环境
?你在用什么?您使用的是Kestral还是IIS?@Jeremythonpson道歉-是的,这是一个
IHostingEnvironment
看起来与RC1不同,并且有一个关于它为空的GitHub错误。你能确认你的代码是这样的:@Jeremythonpson我更新了我的问题
WebrootPath
本身包含我需要的路径,但在调用
path.Combine
之后,它就不再工作了。。。这没有意义h
Path。Combine
对我来说经常做类似的事情。它倾向于其中一个部分以双斜杠或单斜杠结束。