Writing SEO friendly url gets infinite loop asp.net
I am trying to write an SEO url for my site. for this I wrote the following code in my global.asax.
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string oldpath = incoming.Request.Path;
string imgId = string.Empty;
// string imgName = string.Empty;
Regex regex = new Regex(@"N/(.+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = regex.Matches(oldpath);
if (matches.Count > 0)
{
imgId = matches[0].Groups[1].ToString();
// imgName = matches[0].Groups[2].ToString();
string newPath = String.Concat("~/inner.aspx?Id=", imgId);
incoming.RewritePath(String.Concat("~/inner.aspx?Id=", imgId), false);
}
}
But when the regex matches, this code goes into an infinite loop. When I apply the debugger in this code, it moves endlessly when the regex matches. Please help me to solve this problem.
+3
source to share
2 answers
You need to know that your regex is set to ignore the case, so it n
gets caught before the first /
.
You need to either get the last one n
, and whatever is not /
:
Regex regex = new Regex(@"N/([^/]+)$", RegexOptions.IgnoreCase);
Or use case sensitive search:
Regex regex = new Regex(@"N/(.+)");
0
source to share