Server.MapPath and spaces

2.9k views Asked by At

I noticed something weird in Server.MapPath(). If I have a folder with a space at the end I get:

HttpException: Failed to map the path.

This works fine: Server.MapPath("/Folder1/Folder2/item.jpg")

This works fine: Server.MapPath("/Folder1/ Folder2/item.jpg")

This works fine: Server.MapPath("/Folder1/Fol der2/item.jpg")

This fails!: Server.MapPath("/Folder1/Folder2 /item.jpg")

Could somebody explain to me why a space at the end fails while a space anywhere else doesn't?

Note: None of the folders exist.

3

There are 3 answers

1
CodeCaster On BEST ANSWER

Because you shouldn't:

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

The issue comes from the method FileUtil.IsSuspiciousPhysicalPath(string physicalPath, out bool pathTooLong), which does a compare:

string.Compare(physicalPath, Path.GetFullPath(physicalPath), StringComparison.OrdinalIgnoreCase) != 0;

Path.GetFullPath() will trim trailing spaces from directory and file names (because it calls Path.NormalizePath() which does so), which can be discovered calling Path.GetFullPath(@"C:\Foo \Bar.txt") for example. Since that won't match the original path that contains the spaces, the method will return true thus identifying the path as suspicious, after which Server.MapPath will throw the exception.

4
Pranay Rana On

EDIT

Setting up Visual Studio 2010 to step into Microsoft .NET Source Code

Using thing post I tried to find out what is actually haopping in .net framwork when we use method server.MapPath after debugging the .net framwork code I found out calls to get absolute path from virtual path going throw following files and methods of framework

File - System\Web\httpserverutility.cs\1477467\httpserverutility.cs Method-MapPath

File - \System\Web\VirtualPath.cs\1477467\VirtualPath.cs*Method*-Create

File -System\Web\HttpRequest.cs\1599819\HttpRequest.cs Method-MapPath

But i found that with the strage behaviour its returning me null when path is /folder1/folder2 /file.cs but not reutn null object when enter /folder1/ folder2/file.cs

I hop you like to step in framwork and see what happnen

Any way good question ..but still the anwser to this question might be given by microsoft people only who design that files in framewok


The problem is its not able to locate the Folder withg space i.e "Folder2 ".not valid and not able to locate this folder on file system.

Basically you dont find any folder on windows file system which is having sapce after namei.e at the end or begining.

5
mrd On

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server. The method does not check whether the path it returns is valid or exists on the server. You must use Directory.Exists() or File.Exists() method to check if directory or file already exists.

if (File.Exists(Server.MapPath(file)) 

Furthermore, also know following are invlid characters for the method:

Asterisk (*)
Question mark (?)
Angle brackets (< or >)
Comma (,)
Colon or semi-colon (: or ;)
Single-quote or double-quote (' or ")
Right square bracket (])
Double slashes (// or \)

Hope that helps.