我有一个 MVC 应用程序并部署到服务器,我只有 ftp 访问权限,应用程序的名称是 test.foo.com
。有一个后端,用户可以将图片上传到应用程序。一切都很好。代码如下:
//in web config this is the value
<add key="NewsImagesPath" value="~/App_Data/NewsImages/" />
// this is in controller
private static readonly string news_images_path = ConfigurationManager.AppSettings["NewsImagesPath"];
// in the method
String uploadedFile = fileUploadHelper.UploadFile(file, Server.MapPath(news_images_path));
这里是返回上传路径的fileuploadhelper:
public class FileUploadHelper
{
public string UploadFile(HttpPostedFileBase file, string path)
{
if (file != null && file.ContentLength > 0)
{
FileInfo fileInfo = new FileInfo(file.FileName);
string fileName = Guid.NewGuid() + fileInfo.Extension;
var uploadPath = Path.Combine(path, fileName);
file.SaveAs(uploadPath);
return uploadPath;
}
return null;
}
}
这段代码运行良好。
问题出在将此应用程序部署到 foo.com
时。图片仍在上传到 test.foo.com
App_Data 文件夹。
即:我正在从 foo.com 上传图像,图像存储在:
c:\inetpub\wwwroot\test.foo.com\App_Data
而它应该转到
c:\inetpub\wwwroot\foo.com\App_Data
为什么会发生这种情况?
我不知道服务器、IIS是如何配置的。
最佳答案
Server.MapPath("~")
指向配置运行 ASP.NET 应用程序的物理根文件夹。所以我猜想 IIS 中有一些配置,使得 test.foo.com
和 foo.com
实际上都指向同一个应用程序。如果您无权访问服务器来检查这一点,那么除了联系您的托管提供商并询问有关如何设置这些域以及它们映射到哪些应用程序的更多详细信息之外,您无能为力。
关于c# - Server.MapPath 返回错误的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14105108/