gpt4 book ai didi

c# - Server.MapPath 返回不正确的路径

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:49 26 4
gpt4 key购买 nike

我有一个 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.comfoo.com 实际上都指向同一个应用程序。如果您无权访问服务器来检查这一点,那么除了联系您的托管服务提供商并询问有关这些域如何设置以及它们映射到哪些应用程序的更多详细信息之外,您无能为力。

关于c# - Server.MapPath 返回不正确的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14105108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com