2

你好,请帮我解决这个问题

我有以下网址-> www.sample.com/news.aspx?id=45

我想将查询字符串中的“id”传递给 news.aspx 并显示此新闻,但由于 url 重写,url 更改为 --> www.sample.com/news/my-news-45/

如何从查询字符串中提取“id”?

感谢您的帮助

4

1 回答 1

2

您可以手动完成 URL 重写,但手动编写代码的缺点可能是乏味且容易出错。我建议不要自己做,而是使用网络上已经构建的 HttpModules 之一免费为您执行这项工作。

这里有一些免费的,您今天可以下载和使用:

http://urlrewriter.net/ http://www.urlrewriting.net/149/en/home.html

<?xml version="1.0"?>

<configuration>

  <configSections>
    <section name="rewriter"  
             requirePermission="false" 
             type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </configSections>

  <system.web>

    <httpModules>
      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
    </httpModules>

  </system.web>

  <rewriter>
    <rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" />
    <rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" />
    <rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" />
  </rewriter>  

</configuration>  

上面的 HttpModule URL 重写器还添加了对正则表达式和 URL 模式匹配的支持(以避免您必须对 web.config 文件中的每个 URL 进行硬编码)。因此,您无需对类别列表进行硬编码,而是可以重写如下规则,以从任何“/products/[category].aspx”组合的 URL 中动态提取类别:

  <rewriter>
    <rewrite url="~/products/(.+).aspx" to="~/products.aspx?category=$1" />
  </rewriter>  

完整的参考可以在这个链接上找到

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

于 2011-06-21T08:22:49.920 回答