1

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

1 Answer 1

8

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.