1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Web;
5using System.Configuration;
6using System.Xml;
7using System.Text.RegularExpressions;
8using System.Web.UI;
9using System.IO;
10using System.Collections.Specialized;
11
12namespace RewriteModule
13{
14 public class RewriteModuleSectionHandler : IConfigurationSectionHandler
15 {
16
17 private XmlNode _XmlSection;
18
19 private string _RewriteBase;
20
21 private bool _RewriteOn;
22
23
24 public XmlNode XmlSection
25
26 {
27
28 get { return _XmlSection; }
29
30 }
31
32
33
34 public string RewriteBase
35
36 {
37
38 get { return _RewriteBase; }
39
40 }
41
42
43
44 public bool RewriteOn
45
46 {
47
48 get { return _RewriteOn; }
49
50 }
51
52 public object Create(object parent,object configContext, System.Xml.XmlNode section)
53 {
54
55 // set base path for rewriting module to
56
57 // application root
58
59 _RewriteBase = HttpContext.Current.Request.ApplicationPath + "/";
60
61
62
63 // process configuration section
64
65 // from web.config
66
67 try
68
69 {
70 _XmlSection = section;
71 _RewriteOn = Convert.ToBoolean(section.SelectSingleNode("rewriteOn").InnerText);
72
73 }
74
75 catch (Exception ex)
76
77 {
78
79 throw (new Exception("Error while processing RewriteModuleconfiguration section.", ex));
80
81 }
82
83 return this;
84
85 }
86
87 }
88
89
90
91 /////////////////////
92
93
94
95
96 ////////////////////
97
98 class RewriteModule : IHttpModule
99 {
100
101
102
103 public void Dispose() { }
104
105
106
107 public void Init(HttpApplication context)
108 {
109 //it is necessary to
110
111 context.BeginRequest += new EventHandler(
112 RewriteModule_BeginRequest);
113 }
114
115
116 void RewriteModule_BeginRequest(object sender, EventArgs e)
117 {
118
119 RewriteModuleSectionHandler cfg =(RewriteModuleSectionHandler)ConfigurationManager.GetSection("modulesSection/rewriteModule");
120
121
122
123 // module is turned off in web.config
124
125 if (!cfg.RewriteOn) return;
126
127
128
129 string path = HttpContext.Current.Request.Path;
130
131
132
133 // there us nothing to process
134
135 if (path.Length == 0) return;
136
137
138
139 // load rewriting rules from web.config
140
141 // and loop through rules collection until first match
142
143 XmlNode rules = cfg.XmlSection.SelectSingleNode("rewriteRules");
144
145 foreach (XmlNode xml in rules.SelectNodes("rule"))
146 {
147
148 try
149 {
150
151 Regex re = new Regex(
152 cfg.RewriteBase + xml.Attributes["source"].InnerText,
153 RegexOptions.IgnoreCase);
154
155 Match match = re.Match(path);
156
157 if (match.Success)
158 {
159
160 path = re.Replace(
161 path,
162 xml.Attributes["destination"].InnerText);
163
164 if (path.Length != 0)
165 {
166
167 // check for QueryString parameters
168
169 if (HttpContext.Current.Request.QueryString.Count != 0)
170 {
171
172 // if there are Query String papameters
173
174 // then append them to current path
175
176 string sign = (path.IndexOf('?') == -1) ? "?" : "&";
177
178 path = path + sign +
179 HttpContext.Current.Request.QueryString.ToString();
180
181 }
182
183 // new path to rewrite to
184
185 string rew = cfg.RewriteBase + path;
186
187 // save original path to HttpContext for further use
188
189 HttpContext.Current.Items.Add(
190
191 "OriginalUrl",
192
193 HttpContext.Current.Request.RawUrl);
194
195 // rewrite
196
197 HttpContext.Current.RewritePath(rew);
198
199 }
200
201 return;
202
203 }
204
205 }
206
207 catch (Exception ex)
208 {
209
210 throw (new Exception("Incorrect rule.", ex));
211
212 }
213
214 }
215
216 return;
217
218 }
219
220
221
222 }
223
224
225}
226
227
228
229//web.config
230
231<configSections>
232 <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
233 <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
234 <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
235 <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
236 <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
237 <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
238 <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
239 <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
240 </sectionGroup>
241 </sectionGroup>
242 </sectionGroup>
243 <sectionGroup name="modulesSection">
244 <section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
245 </sectionGroup>
246 </configSections>
247
248 <modulesSection>
249 <rewriteModule>
250 <rewriteOn>true</rewriteOn>
251 <rewriteRules>
252 <rule source="(\d+)/(\d+)/(\d+)/" destination="Post.aspx?Year=$1&Month=$2&Day=$3"/>
253 <rule source="(.*)/Default.aspx" destination="Default.aspx?Folder=$1"/>
254 </rewriteRules>
255 </rewriteModule>
256 </modulesSection>
257
258use this link for detail
259http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/
260