URLWrite

添加一个global.asax文件新增如下代码

  protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string oldUrl = HttpContext.Current.Request.RawUrl;
        UrlRewrite(oldUrl, @"^(.*)/Index\.htm", @"Default1.aspx");
        UrlRewrite(oldUrl, @"^(.+)/view-(\d+)-(.+)\.htm(\?.*)*$", "$1/Default2.aspx?id=$2&type=$3");
        UrlRewrite(oldUrl, @"^(.+)/toptic-(.+)\.htm(\?.*)*$", "$1/view/viewDetail.aspx?ID=$2");

        //(@"^(.+)/(\d+)\.htm(\?.*)*$", "$1/Flue/ListView.aspx?id=$2")  任意字符/数字.htm 对应 flue/listview.aspx?id=数字
    }
    private void UrlRewrite(string oldUrl, string pattern, string replace)
    {
        if (Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
        {
            string newUrl = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            this.Context.RewritePath(newUrl);
        }
    }

 

 

、、、、、、、、、、、、、、、、、、、、、、、

 

另外,微软还提供了一个dll文件,URLWriter.dll ,

我们也可以使用它来直接进行URL重写。

①添加引用URLwriter.dll文件

②在web.config文件中,找到<configSections>节点,在结束标志</configSections>前添加代码

第一步
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandlers, URLRewriter">

</configSections>

③在web.config文件中,还是找到<configSections>节点,在结束标志</configSections>后,添加代码

第二步
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/user/blogs.aspx</LookFor>
<SendTo>~/user/blogs.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

④找到节点<httpHandlers>,在<httpHandlers>中添加

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler,URLRewriter"/>
posted @ 2011-11-07 11:29  ajunfly  阅读(1037)  评论(0编辑  收藏  举报