asp.net url 重写解决方案

大家都知道很多网站特别是大型网站现在都用了url伪静态重写来实现用户的易用性和美观性,比如这个网站 http://www.xintaifcw.com 很明显采用了url重写,里面的所有文章内容都是用了http://www.xintaifcw.com/news226.aspx 比如大家看到的这种效果,很明显是采用了asp.net 的url重写方案。

下面废话少说,直接上代码

首先用到微软给我们免费提供的url重写组件:UrlRewriter.dll 这个组件是截获aspx请求,所以我们只能重写成xx.aspx 而不能重写成xx.html 如果要实现xx.html 必须使用iis 的组件才可以实现。

web.confg中配置重写规则

<rewriter>

<rewrite url="~/news(.[0-9]*)\.aspx" to="~/news.aspx?news processing="stop"/>

</rewriter>

规则中我们可以看出 当页面请求 news123.aspx 实际是访问了 news.aspx?newsid=123

        int id = 0;
        if (Request["newsid"] != null)
        {
            if (int.TryParse(Request["newsid"].ToString(), out id) && zf_news_BLLSub.IsExistzf_news(id))
            {
                ae = zf_news_BLLSub.Get_zf_newsEntity(id);

             }

  

所以在news.aspx 中加入的处理事件和我们平常的写法没有区别。

如此简单就实现了url重写,url重写也在seo中有重要作用。