简体   繁体   English

ASP.NET:Server.MapPath返回字符串NOT_A_VALID_FILESYSTEM_PATH

[英]ASP.NET: Server.MapPath returns the string NOT_A_VALID_FILESYSTEM_PATH

In my ASP.NET application, a method that is supposed to write a file to disk sometimes unexpectedly ends up writing the file with the filename NOT_A_VALID_FILESYSTEM_PATH . 在我的ASP.NET应用程序中,应该将文件写入磁盘的方法有时会意外地以文件名NOT_A_VALID_FILESYSTEM_PATH写入文件。

The (simplified) code is: (简化的)代码是:

private void WriteFile(string fileID)
{
    string fileName = Server.MapPath("/Data/" + fileID + ".xml");

    // (More code that writes data to that file...)
}

Why is Server.MapPath returning the string "NOT_A_VALID_FILESYSTEM_PATH"? 为什么Server.MapPath返回字符串“ NOT_A_VALID_FILESYSTEM_PATH”?

Server.MapPath returns the string "NOT_A_VALID_FILESYSTEM_PATH" when both of the following are true: 满足以下两个条件时, Server.MapPath返回字符串“ NOT_A_VALID_FILESYSTEM_PATH”:

  1. The argument provided to Server.MapPath includes a character that can't appear in a valid filename, such as colon (":") or question mark ("?"); 提供给Server.MapPath的参数包含一个不能出现在有效文件名中的字符,例如冒号(“:”)或问号(“?”);
  2. In the application's web.config file, the system.web / httpRuntime element includes the attribute relaxedUrlToFileSystemMapping="true" . 在应用程序的web.config文件中,system.web / httpRuntime元素包括属性RelaxUrlToFileSystemMapping =“ true”

If the web.config doesn't have the relaxedUrlToFileSystemMapping="true" , then the call to Server.MapPath will instead throw a System.NotSupportedException with the message "The given path's format is not supported." 如果web.config不具有relaxedUrlToFileSystemMapping="true" ,则对Server.MapPath的调用将引发System.NotSupportedException并显示消息“不支持给定路径的格式”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM