Url Rewriting in ASP.NET 2

Just starting to migrate some of my projects to .Net 2.0 and I needed to figure out how to migrate the URL rewriting I used to code through an HTTP module.

Well easy with ASP.NET 2.0! Just edit the weconfig file and do this:

<system.web>
    <urlMappings enabled ="true">
       <add url="~/bananas.aspx" mappedUrl="Product.aspx?display=bananas"/>
    </urlMappings>
</system.web>

And even better you can use some regular expressions like this:

<RewriterConfig>
  <Rules>
<RewriterRule>
<LookFor>~/(\d{4})/Default\.aspx</LookFor>
<SendTo>~/Report.aspx?year=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

 

No Comments