Server.MapPath C# 在本地工作但不能从服务器工作

Server.MapPath C# works locally but not from server

Server.MapPath 有一个很大的问题,因为我无法从服务器解析它并且不确定如何测试它正在尝试做什么。

在本地,它以各种不同的方式工作得很好,但是当我把它放到服务器上时,每一个都失败了。

string filename = "TestLog.txt";
string path = Path.Combine(Server.MapPath("~/Content/"), filename);
Debug.WriteLine(path)

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path, true))
{
    writer.WriteLine(message);
    writer.Close();
}

我已经尝试在路径中添加 /content 并将额外的 / 添加到文件名中。尝试过使用和不使用 tilda。他们都在本地正确解析了路径,但在服务器上却没有。

郑重声明,我尝试访问的路径是

本地:C:\Users\username\Documents\MyWebSite\Web\Content\TestLog.txt 服务器:D:\Web\MyWebSite\Content\TestLog.txt

MyWebSite\Web 是本地站点的根 MyWebSite 是服务器上的根,所以 mywebsite.com/content/TestLog.txt

非常感谢任何帮助。

这将是一个权限问题。

当您在 运行 本地时(在 Cassini WebServer 下...Visual Studio 的一部分),您已获得许可。

当您在 IIS 中托管时,您可能不会。

向该文件夹授予 "write" 权限 - 一切都取决于您使用的 IIS 版本 - 可能是通过 ApplicationPool。

How do I give ASP.NET permission to write to a folder in Windows 7?

IIS7 folder permissions for web application