1

I am working on a web application. Here I am storing attachments/uploads on server physical directory. The parent folder of uploads may contain special characters like '?'

Example of URL

"~/ChapterFiles/Capgeminisdfsdf_BE CSE ?_CoverPic/CoverPic.jpg"

When I am doing, Server.MapPath() on this URL, I am getting an error "Illegal characters in path."

Can't remove question mark from folder name as it's part of requirement. Please suggest a solution, I need to fix it urgently.

Wasif Subzwari
  • 199
  • 3
  • 13

4 Answers4

0

You can use something like:

String absoluteDir = Server.MapPath("~");
String myRelativePath = "~/ChapterFiles/Capgeminisdfsdf_BE CSE ?  _CoverPic/CoverPic.jpg".Replace("/","\\");

String absolutePath = Path.Combine(absoluteDir,myRelativePath);

It will work. I advice you to write some unit tests for this function.

0

Use HttpServerUtility.UrlEncode and UrlDecode to encode/decode the string.

Hintham
  • 1,078
  • 10
  • 29
0

Question marks are not allowed in folder names in Windows. Your requirement in it's current form is impossible to implement and there is no "fix". You need to rethink how to map URL's to folder and file names.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
-1

You need to use @ sign before the string. Like below

@"~/ChapterFiles/Capgeminisdfsdf_BE CSE ?_CoverPic/CoverPic.jpg"

Reference link

Community
  • 1
  • 1
Nad
  • 4,605
  • 11
  • 71
  • 160