Jon Schneider
Jon Schneider

Reputation: 26993

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.

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"?

Upvotes: 1

Views: 1277

Answers (1)

Jon Schneider
Jon Schneider

Reputation: 26993

Server.MapPath returns the string "NOT_A_VALID_FILESYSTEM_PATH" when both of the following are true:

  1. The argument provided to Server.MapPath includes a character that can't appear in a valid filename, such as colon (":") or question mark ("?");
  2. In the application's web.config file, the system.web / httpRuntime element includes the attribute relaxedUrlToFileSystemMapping="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."

Upvotes: 8

Related Questions