【问题标题】:Server.MapPath C# works locally but not from serverServer.MapPath C# 在本地工作,但不能从服务器工作
【发布时间】:2017-04-11 09:44:46
【问题描述】:

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\用户名\Documents\MyWebSite\Web\Content\TestLog.txt 服务器:D:\Web\MyWebSite\Content\TestLog.txt

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

非常感谢任何帮助。

【问题讨论】:

    标签: c# server.mappath


    【解决方案1】:

    这将是一个权限问题。

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

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

    授予该文件夹“写入”权限 - 完全取决于您使用的 IIS 版本 - 可能是通过 ApplicationPool。

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

    IIS7 folder permissions for web application

    【讨论】:

    • 谢谢伙计。给你一个胖绿勾号。
    猜你喜欢
    • 2021-10-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多