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.
You need to use
@ sign before the string. Like below
@"~/ChapterFiles/Capgeminisdfsdf_BE CSE ?_CoverPic/CoverPic.jpg"
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.
UrlDecode to encode/decode the string.
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.