c# - 写 SEO 友好的 url 得到无限循环 asp.net

标签 c# asp.net regex url-rewriting

我正在尝试为我的网站编写 SEO 友好的 URL。为此,我在我的 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);
        }

    }

但是当正则表达式匹配时,这段代码会无限循环。当我在此代码中应用调试器时,它会在正则表达式匹配时无限移动。请帮我解决这个问题。

最佳答案

看来问题与正则表达式有关。

这可能是由于过多的回溯。查看有关回溯的更多信息 here

如果您使用的是 ASP.Net 4.5 版,请尝试使用 .NET 4.5 的新正则表达式超时功能 here

关于c# - 写 SEO 友好的 url 得到无限循环 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30439073/

相关文章:

c# - 只读打开文件对话框

c# - 继承 .NET Core 中的 appsettings.json 设置

c# - 使用 ReaderWriterLock 的真正缺点是什么

asp.net - 如何调试 ASP.NET 编译错误?

c# - GridView 不显示所有行

java - 正则表达式确定文本中某个单词的出现次数是否不超过 n 次

Java:使用正则表达式查找 URL 将它们转换为 html 链接。同时检测链接是否包含http://,如果没有,追加它

c# - 关于多用户全局变量的思考

javascript - 我无法访问 <asp :textbox> value in code behind file

regex - PowerShell - 正则表达式将匹配结果放入变量