I was installing the Simple Counter Extension for BlogEngine.Net (created by Al Nyveldt) today when I ran into a slight glitch. My download counts were being tallied properly in the downloadcounts.xml file. However, when I logged in to check my download counts on each link they all said “0”.
That couldn’t be right so I refreshed my cache and still nothing. The problem was that the string in the downloadcounts.xml file was not matching the string from the generated html.
Here are the two strings:
2: <file count="1">2009/6/facePLANT.zip</file>
1: <a href="/file.axd?file=2009%2f6%2ffacePLANT.zip">
The solution was to add one line code to the SimpleDownloadCounter.cs file. Just open the file and update the UpdateDisplay method:
1: private string UpdateDisplay(string body)
3: if (body.Contains("file.axd?file="))
5: int pos = body.IndexOf("file.axd?file=");
6: while (pos > 0)
8: pos = pos + 14;
9: string filename = body.Substring(pos, body.IndexOf("\"", pos) - pos);
10: filename = Uri.UnescapeDataString(filename);
11: int count = GetFileCount(filename);
12: int linkTextEnds = body.IndexOf("</a>", pos);
13: body = body.Insert(linkTextEnds, " [Downloads: " + count.ToString() + "]");
14: pos = body.IndexOf("file.axd?file=", pos);
17: return body;
I added line 10:
1: filename = Uri.UnescapeDataString(filename);
This will remove the URL Encoding from the HTML link before it tries to look it up in the xml file. Now the strings match and the counter works as intended.
Wed, June 24, 2009
by Keith Storm