C# 服务器的映像文件路径不是C:驱动器MVC5

C# 服务器的映像文件路径不是C:驱动器MVC5,c#,image,entity-framework,asp.net-mvc-5,C#,Image,Entity Framework,Asp.net Mvc 5,我有一个上传图像的应用程序,我试图获取存储在服务器上的路径,而不是在我的C:驱动器上。以下是控制器中的代码: if (ModelState.IsValid) { if (file != null) { string ImageName = System.IO.Path.GetFileName(file.FileName); string physicalPath = Server.MapPath(

我有一个上传图像的应用程序,我试图获取存储在服务器上的路径,而不是在我的C:驱动器上。以下是控制器中的代码:

 if (ModelState.IsValid)
        {

        if (file != null)
        {
            string ImageName = System.IO.Path.GetFileName(file.FileName);
            string physicalPath = Server.MapPath("~/Images/");

            try
            {
                if (!Directory.Exists(physicalPath))
                    Directory.CreateDirectory(physicalPath);

                string physicalFullPath = Path.Combine(physicalPath, ImageName);

                file.SaveAs(physicalFullPath);

                customer.CustomerLogo = ImageName;
                customer.CustomerLogoPath = physicalFullPath;
                db.Customers.Add(customer);
                db.SaveChanges();
            }
            catch(Exception e)
            {
                return View("Error",e.Message );
            }
        }
        return RedirectToAction("Index");

    }
    return View(customer);
}

它当前是这样存储的(请参见上图),我需要路径为“admin.loyaltyworx.co.za\Images\jeep.jpg”,如何实现这一点?

使用

string physicalPath = "c:\\admin.loyaltyworx.co.za\\Images";
而不是

string physicalPath = Server.MapPath("~/Images/");

我不想它存储在我的C驱动器上,直接到服务器我不明白,网站在服务器上,对吗?然后,它将使用服务器的驱动器
C:
。我尝试了您的方法,它返回了一个错误:“C:/admin.loyaltyworx.co.za/Images”是一个物理路径,但虚拟路径无效expected@Unicorn_Girl我更正了我的答案。请看一下。你换了什么?这是一个同样可能重复的问题。请检查以下内容:您接受了一个完全错误的答案。您需要使用
Server.MapPath(“~/Images/”)否则,当您尝试读取图像时,您将永远无法访问该图像(并且您只能看到该路径,因为您在本地计算机上创建了图像,当然,一旦发布应用程序,您将无法访问该图像)。@StephenMuecke,我接受的答案我当然试过了,甚至在我发表之后它仍然有效。这是很难编码的路径。不要那样做!啊,我明白了,当我将它保留为Server.MapPath(“~/Images/”)时,它没有保存到服务器上,一旦发布就无法读取。你对更好的解决方案有什么建议吗?