I have the below regex which I used to perform redirections
string requestedPath = HttpUtility.UrlDecode(this.StripLanguage(currentContext.InputUrl.AbsolutePath));
string requestedPathAndQuery = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedRawUrl = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedUrl =
HttpUtility.UrlDecode(
string.Concat(
currentContext.InputUrl.Scheme,
"://",
currentContext.InputUrl.Host,
requestedRawUrl));
string requestedRawUrlDomainAppended = HttpUtility.UrlDecode(currentContext.InputUrl.AbsoluteUri);
string requestedPathWithCulture = HttpUtility.UrlDecode(currentContext.InputUrl.AbsolutePath);
var finalRequestedURL = string.Empty;
finalRequestedURL = Regex.IsMatch(requestedPathAndQuery,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedPathAndQuery
: Regex.IsMatch(requestedPath,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedPath
: Regex.IsMatch(requestedPathWithCulture,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedPathWithCulture
: Regex.IsMatch(requestedRawUrl,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedRawUrl
: Regex.IsMatch(requestedUrl,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedRawUrlDomainAppended
: string.Empty;
The matchPattern variable is the Url. Example: (.*)/articles/my-article(.*) should redirect to http://www.google.com
The regex works fine but when it comes to lots of requests, our CPU goes to 100%.
Is there any solution to optimized the above?
Thanks
String.Contains("/articles/my-atricle")and skip theregexaltogether.matchPattern's are there? You can try to compile them and store in dictionary based on pattern. Also movingmatchPattern.Trim()to separate variable will not solve the issue but still is nice.Regexand reuse it.