ASP.NET实现伪静态

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Text;
 6 using System.Text.RegularExpressions;
 7 
 8 /// <summary>
 9 ///UrlRewriter 伪静态Url重写
10 /// </summary>
11 public class UrlRewriter:IHttpHandler
12 {
13     /// <summary>
14     /// 自定义 HttpHandler
15     /// </summary>
16     /// <param name="context"></param>
17     public void ProcessRequest(HttpContext context)
18     {
19         try
20         {
21             string url = context.Request.RawUrl;//获取用户请求的URL地址信息
22             Regex Reg = new Regex(@"/detail-(\d+)-(\d+)\..+", RegexOptions.IgnoreCase);//建立正则表达式 
23             Match m = Reg.Match(url, url.LastIndexOf("/"));//用正则表达式进行URL字符串
24             if (m.Success)//匹配成功
25             {
26                 string RealPath = @"~/admin/detail.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2];//重定向真实的地址信息
27                 context.Server.Execute(RealPath);
28             }
29             else
30             {
31                 context.Response.Redirect(context.Request.Url.ToString());
32             }
33 
34         }
35         catch (Exception ex)
36         { 
37             context.Response.Redirect(context.Request.Url.ToString());  
38         }
39     }
40 
41     /// <summary>  
42     ///  如果 System.Web.IHttpHandler 实例可再次使用,则为 true;否则为 false。
43     /// </summary>  
44     public bool IsReusable
45     {
46         get { return false; }
47     }
48 }


 1 <?xml version="1.0"?>
 2 <configuration>
 3 <system.web>
 4   <httpHandlers>
 5     <!--使用自定义UrlRewriter类-->
 6     <add verb="*" path="*/detail-?*-?*.html" type="UrlRewriter"/>
 7     </httpHandlers>
 8     <compilation debug="true" targetFramework="4.0"/>
 9   </system.web>
10 </configuration>


 1 <!--使用微软URLRewriter.dll 配置文件-->
 2 <?xml version="1.0"?>
 3 <configuration>
 4   <!--使用URLRewriter.dll -->
 5 <configSections>
 6   <section name="RewriterConfig" requirePermission="false" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
 7 </configSections>
 8 
 9 <RewriterConfig>
10   <Rules>
11     <RewriterRule>
12       <LookFor>~/detail/([0-9]*)/([0-9]*).html</LookFor>
13       <SendTo>~/admin/detail.aspx?type=$1&amp;id=$2</SendTo>
14     </RewriterRule>
15   </Rules>
16 </RewriterConfig>
17 
18 <system.web>
19   <httpHandlers>
20     <!--使用URLRewriter.dll    -->
21   <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
22   <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
23 
24 </httpHandlers>
25     <compilation debug="true" targetFramework="4.0"/>
26   </system.web>
27 </configuration>

 

 

 

posted @ 2015-05-26 16:21  Bill 李  阅读(227)  评论(0编辑  收藏  举报