c# - Server.MapPath 返回错误的路径

标签 c# asp.net-mvc asp.net-mvc-3 iis

我有一个 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/

相关文章:

c# - 如何使用 ASP.Net MVC 制作向导

c# - 访问表达式主体成员以构建表达式树

c# - 更改 Nhibernate 连接字符串

c# - 如何用单个 <BR> 替换多个换行符?

asp.net-mvc - 将数据从 html 编辑器发布到 MVC 中的 Controller 操作

razor - 将 MVC 2.0 迁移到 MVC 3.0 RC 后使用 Razor 时出现问题

c# - 关于 Linq to SQL 映射对象设计的建议

c# - SignalR 和异构类型的客户端

asp.net - 请求重定向到/Account/Login?ReturnUrl=%2f,因为服务器上安装了 MVC 3

asp.net-mvc-3 - 如何在@Html.Partial 中传递嵌套模型值