Removing %20 from URI Relative Path

半世苍凉 提交于 2019-11-30 00:25:41

问题


I am generating a relative path from 1 directory to another. If the OutputDirectoryName property is a directory containing spaces, the spaces are encoded using %20, rather than a space. I am creating a relative path to a windows folder, so I must have my relatiave path using spaces. Is there a clean way to specify how the URI is encoded? I know I could do a stirng replace on the relativePath.ToString(), but am wondering if there's a better implementation. Thanks.

public string GetOutputDirectoryAsRelativePath(string baseDirectory)
{
    Uri baseUri = new Uri(baseDirectory);
    Uri destinationUri = new Uri(OutputDirectoryName);
    Uri relativePath = baseUri.MakeRelativeUri(destinationUri);
    return relativePath.ToString();
}

回答1:


Use Uri.UnescapeDataString?

http://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring.aspx




回答2:


string sRelativeFilePath = Uri.UnescapeDataString(new Uri(sAbsolutePath + "\\", false).MakeRelative(new Uri(filename)));



回答3:


Use HttpServerUtility.UrlDecode Method (String)




回答4:


Try looking at Server.UrlDecode: http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

The space character is not the only one that is encoded.



来源:https://stackoverflow.com/questions/5706555/removing-20-from-uri-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!