Most URL rewriters out there only let you rewrite requested URL's to internal URL's. In practice,
you often see people create/generate friendly URL's and then create mappings for those friendly
URL's to internal URL's. Whenever you would decide to change your friendly URL's, you have a
problem. You have to change the mappings AND the created/generated URL's.
To overcome this problem, I put together a 2-way URL rewriter. It lets you define mappings from
a friendly url to the real url, and vice versa. The default web config rule provider lets you
define the mappings as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<configuration>
<configSections>
<section name="rewriter" type="Wilco.Configuration.ProviderSectionHandler, Wilco.Configuration" />
</configSections>
<rewriter>
<provider type="Wilco.Web.RewriteEngine.Providers.WebConfigRuleProvider, Wilco.Web.RewriteEngine">
<rules>
<rule>
<!-- /blah.aspx - /default.aspx -->
<inbound pattern="~/blah.aspx" replacement="~/default.aspx" />
<outbound pattern="~/default.aspx" replacement="~/blah.aspx" />
</rule>
<rule>
<!-- /news/123 - /news.aspx?newsid=123 -->
<inbound pattern="~/news/(\d+)$" replacement="~/news.aspx?newsid=$1" />
<outbound pattern="~/news.aspx\?newsid=(\d+)$" replacement="~/news/$1" />
</rule>
</rules>
</provider>
</rewriter>
<httpModules>
<add name="Rewriter" type="Wilco.Web.RewriteEngine.Rewriter, Wilco.Web.RewriteEngine" />
</httpModules>
</configuration>
|
The rewriter comes with an HttpModule which takes care incoming URL's. Internally it relies on the helper
method 'string UrlUtility.ConvertToRealUrl(string friendlyUrl)'. This helper class also contains a
method 'string UrlUtility.ConvertToFriendlyUrl(string realUrl)', which does the obvious.
Custom providers can be implemented, by either inheriting from the web config rule provider or implementing
IRuleProvider. You can download the binaries and source for free.
License:
This project is released under Ms-PL. Please contact me directly for questions or exceptions.
Update:
Please have a look at my HtmlFormEx implementation which should be used together with this component to solve an issue with postbacks.