
The problem above is that the URL/Path passed is already URL encoded, and one way to fix this is the pass in the URLas an unencoded string: var part1 = new Uri("C:\\temp\\assets\\Image File.jpg") // unencoded This creates incorrectly encoded values for AbsoluteUrl and LocalPath: file:///c:/temp/Image%2520File.jpgīoth of which are obviously not what is desired here. Url = uri.AbsoluteUri // Wrong: file:///c:/temp/Image%2520File.jpg Uri.Dump() // shows correctly as it shows original url Var part2 = "assets/Image%20File.jpg" // src/href from HTML doc Var part1 = new Uri("C:\\temp\\") // double encodes when combining parts
SITESUCKER DUMP INTO WORDPRESS CODE
In the example code above I essentially assigned the Uri like this: var baseUri = new Uri("c:\\temp\\") Specifically, the protocol - or lack thereof - determines how the Uri is treated for file:/// urls. Uri Constructor: Scheme MattersĪfter a bit of experimenting it turns out that the problem is how the Uri instance is initialized from a string. IOW, the input is treated as a raw unencoded string. The result is that the %20 space encoding is encoded as %2520 - basically encoding the % and writing out the 20 after that. Specifically when combining the URLs, the second URL is added as a literal string and not considered as encoded. Unfortunately, this code runs into a serious problem with the Url Encoding: ImageData = File.ReadAllBytes(uri.LocalPath) ImageData = http.DownloadData(uri.AbsoluteUri) Var url = uri.AbsoluteUri // here things go awry! then figure out relative path to retrieve resources Var basePath = Path.GetDirectoryName(doc.Filename) Seems straight forward enough: // assign base path initially from a filename If the path is relative it's assumed to be relative to the current document, and I use the URI class to combine the base path with the relative path. I use Uri because the relative path may be relative to an online resource or a local file. To do this a document relative path is required, which in this case I build up using the URI class. My specific scenario in code is that in Markdown Monster I have an output generation class that dumps a generated HTML document as a self contained file to disk by pulling in all related resources and embedding them directly into one large document. This particular issue affects file:/// urls and it concerns different behavior for Url Encoding depending on how a file URL is initialized in the Uri class. One very nice thing about the URI class is that it also works with file paths so it can be quite useful into combining paths and encoding/decoding the URL formatting which is quite useful if you're embedding local file links into things like Markdown documents (imagine that!). Specifically I commonly use it to normalize relative URLs into absolute Urls or vice versa. I frequently use the Uri class to build Urls, both for Web Urls as well as for local Urls. It's great when it works as you expect, but I've had a few of battles related to Url encoding and decoding and in this post I'll point out one oddity that bit me today. I have a love/hate relationship with the System.Uri class.
