问题描述:

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"

Reference link

网友答案:

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.

网友答案:

Use HttpServerUtility.UrlEncode and 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.

相关阅读:
Top