C# 尝试访问我的应用程序文件夹中的路径时出现“找不到路径”错误
Net核心web应用程序,该应用程序具有一个实用程序,用户可以在其中上载照片。它使用一个API控制器来完成实际的上传。我在名为C# 尝试访问我的应用程序文件夹中的路径时出现“找不到路径”错误,c#,asp.net-core,asp.net-core-webapi,C#,Asp.net Core,Asp.net Core Webapi,Net核心web应用程序,该应用程序具有一个实用程序,用户可以在其中上载照片。它使用一个API控制器来完成实际的上传。我在名为ImageStorage的解决方案中创建了一个文件夹,其中文件夹路径如下所示: G:\GameDevelopment\DungeonMaxer\MainApp\wwwroot\ImageStorage 我还在ImageStorage下创建了一个名为uploads的文件夹 我正在尝试在本地(IIS Express)进行测试,并在控制器中添加了以下代码行: [HttpPost
ImageStorage
的解决方案中创建了一个文件夹,其中文件夹路径如下所示:
G:\GameDevelopment\DungeonMaxer\MainApp\wwwroot\ImageStorage
我还在ImageStorage下创建了一个名为uploads的文件夹
我正在尝试在本地(IIS Express)进行测试,并在控制器中添加了以下代码行:
[HttpPost("upload")]
public async Task<IActionResult> PostAsync(IFormFile file)
{
// parent folder for storage
var serverStoragePath = Server.MapPath(@"~/ImageStorage");
// uploads folder inside the parent
var fileRoute = Path.Combine(serverStoragePath, "uploads");
// get file extension
string extension = System.IO.Path.GetExtension(theFile.FileName);
// create a new name for storage
string name = Guid.NewGuid().ToString().Substring(0, 8) + extension;
// get the link to the file
string link = Path.Combine(fileRoute, name);
using (FileStream writerFileStream = System.IO.File.Create(link))
{
await stream.CopyToAsync(writerFileStream);
writerFileStream.Dispose();
}
// Return the file path as json
Hashtable imageUrl = new Hashtable();
imageUrl.Add("link", "/uploads/" + name);
return Json(imageUrl);
}
我还尝试了wwwroot\ImageStorage
,但这给了我同样的错误
有办法进入这条路吗
谢谢 IHostingEnvironment在.Net Core 2.0中使用 IWebHostEnvironment已经取代了.NETCore3.0中的IHostingEnvironment
public class HomeController : Controller
{
private IHostingEnvironment Environment;
public HomeController(IHostingEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
string wwwPath = this.Environment.WebRootPath;
string contentPath = this.Environment.ContentRootPath;
return View();
}
}
IHostingEnvironment用于.NETCore2.0 IWebHostEnvironment已经取代了.NETCore3.0中的IHostingEnvironment
public class HomeController : Controller
{
private IHostingEnvironment Environment;
public HomeController(IHostingEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
string wwwPath = this.Environment.WebRootPath;
string contentPath = this.Environment.ContentRootPath;
return View();
}
}
这可能只是许可问题吗?我假设您已经使用调试器检查了最终文件名是否正确。@Steve我知道路径存在于我的文件系统中。我还在Microsoft站点上查找了Server.MapPath,我认为我使用它是正确的。在调用Server.MapPath之后,serverStoragePath的值是多少?。。。也可能不是应用程序基本路径设置为等待的位置。您正在尝试在Asp.Net核心项目中使用Server.MapPath吗?不行,您需要通过DI使用IWebHostEnvironment,然后使用属性WebRootPathJust Path。将WebRootPath与文件夹结合起来/s这是否只是权限问题?我假设您已经使用调试器检查了最终文件名是否正确。@Steve我知道路径存在于我的文件系统中。我还在Microsoft站点上查找了Server.MapPath,我认为我使用它是正确的。在调用Server.MapPath之后,serverStoragePath的值是多少?。。。也可能不是应用程序基本路径设置为等待的位置。您正在尝试在Asp.Net核心项目中使用Server.MapPath吗?不行,您需要通过DI使用IWebHostEnvironment,然后使用属性WebRootPathJust Path。将WebRootPath与您的文件夹结合起来/sOh我明白了,但是如何获得名为“
Server.MapPath”(@“~/ImageStorage”)代码>?谢谢,在哪里定义environment.webRootPath?这在Startup.cs中吗?谢谢,您可以在控制器构造函数中初始化环境,然后在PostAsync操作中使用wwwPath。哦,我明白了,但我如何获得名为,Server.MapPath(@“~/ImageStorage”)代码>?谢谢,在哪里定义environment.webRootPath?这在Startup.cs中吗?感谢您可以在控制器构造函数中初始化环境,然后在PostAsync操作中使用wwwPath。