asp.net url重写非重定向

1.    重写操作类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Text.RegularExpressions;
 6 using System.Web;
 7 using System.Configuration;
 8 
 9 namespace T9.Util
10 {
11     public class UrlReWriter : IHttpModule
12     {
13         public void Init(HttpApplication context)
14         {
15             context.BeginRequest += new EventHandler(ContextOnBeginRequest);
16         }
17         [Serializable]
18         public class RewriteConfig
19         {
20             public string LookFor;
21             public string ReplaceTo;
22         }
23         private void ContextOnBeginRequest(object sender, EventArgs eventArgs)
24         {
25             string strRewriteConfigs = ConfigurationManager.AppSettings["RewriteConfigs"];
26             if (string.IsNullOrWhiteSpace(strRewriteConfigs))
27             {
28                 return;
29             }
30             List<RewriteConfig> listRewriteConfigs = T9.Util.JSONUtil.JsonDeserialize<List<RewriteConfig>>(HttpUtility.UrlDecode(strRewriteConfigs));
31             HttpApplication application = sender as HttpApplication;
32             HttpContext context = application.Context;
33             string url = context.Request.Url.LocalPath;
34             foreach (RewriteConfig config in listRewriteConfigs)
35             {
36                 if (url.Contains(config.LookFor))
37                 {
38                     context.RewritePath(url.Replace(config.LookFor, config.ReplaceTo));
39                 }
40             }
41         }
42 
43         public void Dispose()
44         {
45             //throw new NotImplementedException();
46         }
47     }
48 
49 }

 

2.    在配置文件中system.webServer标签下新增如下配置

 

1   <system.webServer>
2     <modules>
3       <add name="UrlReWriter" type="T9.Util.UrlReWriter,T9.Util"/>
4     </modules>
5   </system.webServer>

 

3.    配置文件appSettings下新增url重写配置

1 <add key="RewriteConfigs" value="%5B%7B%22LookFor%22%3A %22huawei%2Fhealthkit%2Fnotifications%22%2C%22ReplaceTo%22%3A %22huawei%2Fnotifications.ashx%22%7D%5D"/>

 

4.    有问题联系我vx

 

posted @ 2021-06-10 16:33  杰煕  阅读(62)  评论(0)    收藏  举报