找不到路径C#asp.net的一部分,路径在那里

找不到路径C#asp.net的一部分,路径在那里,c#,asp.net,C#,Asp.net,迷路了 我有以下代码: public ActionResult UploadCV(string userId, HttpPostedFileBase cvfile) { if (cvfile != null && cvfile.ContentLength > 0) { var bareFilename = Path.GetFileNameWithoutExtension(cvfi

迷路了

我有以下代码:

public ActionResult UploadCV(string userId, HttpPostedFileBase cvfile)
    {
        if (cvfile != null && cvfile.ContentLength > 0)
        {                
            var bareFilename = Path.GetFileNameWithoutExtension(cvfile.FileName);
            var fileExt = Path.GetExtension(cvfile.FileName);
            //save to dir first
            var path = Path.Combine(Server.MapPath("/App_Data/userfiles/"),
                                    userId + "/");
            var dir = Directory.CreateDirectory(path);
            cvfile.SaveAs(path);
            //save to db..
            var user = Ctx.Users.FirstOrDefault(x => x.Id == userId);
            user.CvLocation = "/App_Data/userfiles/" + userId + "/" + bareFilename + fileExt;
            user.CvUploadDate = DateTime.Now;
            Ctx.SaveChanges();

        }
        return RedirectToAction("Index", "Settings", new { area = "" });
    }
在线:

cvfile.SaveAs(path);
我得到了错误

Could not find a part of the path 
'C:\Users\me\big\ol\path\App_Data\userfiles\4cf86a2c-619b-402a-80db-cc1e13e5288f\'.
如果我在资源管理器中导航到路径,它会显示良好

我试图解决的是按照用户在数据库中的唯一GUID对用户上传进行排序。我希望文件夹“userfiles”具有用户guid的文件夹名称,并且在该文件夹中具有“mycolpic.png”


你知道我做错了什么吗?

你试图保存而不提供文件名。需要一个表示文件名而不仅仅是路径的参数。 只需将服务器
路径
与前面提取的
文件名
结合使用即可

 var path = Path.Combine(Server.MapPath, "/App_Data/userfiles/"),userId);
 var dir = Directory.CreateDirectory(path);
 cvfile.SaveAs(Path.Combine(path, bareFilename);

您正在尝试在不提供文件名的情况下保存。需要一个表示文件名而不仅仅是路径的参数。 只需将服务器
路径
与前面提取的
文件名
结合使用即可

 var path = Path.Combine(Server.MapPath, "/App_Data/userfiles/"),userId);
 var dir = Directory.CreateDirectory(path);
 cvfile.SaveAs(Path.Combine(path, bareFilename);

我在生产环境中遇到了这个问题,但在开发过程中没有

我的问题是因为我忽略了
~
符号

我从这里开始:

var path = Server.MapPath("/App_Data/templates/registration.html");
为此:

var path = Server.MapPath("~/App_Data/templates/registration.html");

我在生产环境中遇到了这个问题,但在开发过程中没有

我的问题是因为我忽略了
~
符号

我从这里开始:

var path = Server.MapPath("/App_Data/templates/registration.html");
为此:

var path = Server.MapPath("~/App_Data/templates/registration.html");

通常,这意味着存在权限问题。当运行时没有访问资源的权限时,它会引发未找到的异常。如果保存映像,则需要添加其文件名:
Path.Combine(Path,“mycolpic.png”)
,而不是将其保存为目录。通常,这意味着存在权限问题。当运行时没有访问资源的权限时,它会引发未找到的异常。如果保存映像,则需要添加其文件名:
Path.Combine(Path,“mycolpic.png”)
,而不是将其保存为目录。