In this code, you scan a string three times for a character:
if (virtualPath.Contains("?")) { condition = virtualPath.Substring(virtualPath.IndexOf("?")); virtualPath = virtualPath.Substring(0, virtualPath.IndexOf("?")); }
Instead, scan it once and use the result three times. Also scan char instead of string:
int pos = virtualPath.IndexOf('?'); if (pos != -1) { condition = virtualPath.Substring(pos); virtualPath = virtualPath.Substring(0, pos); }
Here you make several replacements with the same replacement:
virtualPath = virtualPath.ToLower().Replace(",", "-").Replace("%20", "-").Replace("&", "-");
Instead, you can use regex for all of them:
virtualPath = Regex.Replace(virtualPath.ToLower(), "(,|%20|&)", "-");
(Regardless of whether this actually gives the best performance, you need to check some of your actual data. Although this is less than the operations, there is some overhead when setting up the regular expression.)
You use a loop to reduce character clusters:
while (virtualPath.Contains("--")) { virtualPath = virtualPath.Replace("--", "-"); }
Instead, you can use regex to create one replacement:
virtualPath = Regex.Replace(virtualPath, "-{2,}", "-");