There are many Url Rewriting Components are available on net for ASP.NET, i used many of them but all are some pros and cons. The major issues using these components is HTTP 302 Error. At last i found a component that i used in my many ASP.NET applications and its best. The component is UrlRewritingNet.UrlRewrite .
You can easily find this component on internet, its absolutely free. There is also a complete documentation is available with it. Which contains detail information how it use. However i provide you some information about it there in my article.
What is UrlRewritingNet.UrlRewrite?
UrlRewritingNet.UrlRewrite is a module which could be embedded in an ASP.NET 2.0 application to rewrite Urls (Internet addresses) for displaying the user another URL than used from the server. With UrlRewritingNet.UrlRewrite you only have to define a few simple rules for rewriting your addresses.
Why rewriting my Urls?
A little example. You’re developing blog software which stores its entries in a database. To get the entry for displaying it in a details page you need the identity (ID) of the record to display. To get the ID you usually transport this value by Query String: http://myblog.com/details.aspx?id=234. If your blog is ready and online you want to be found by potential readers on search engines like Google or Yahoo. These search engines send bots out to the World Wide Web to find interesting content. So what do you mean what the bot does with a Url like shown above? Not much, right.
So, wouldn’t it be cooler if the bot could find the topic of the blog entry in the Url for example? A rewritten Url could look like this: http://myblog.com/detail/good-news-for-a-better-world-234.aspx. The machine (search engine bot) has something to analyze and the user can imagine what the topic is about on this page, too.
Features that UrlRewritingNet.UrlRewrite Support:
- Rewriting Urls based on regular expressions
- Support for Themes and Master Pages
- Support for OutputCacheing
- Use in medium trust level environments (shared hosting) possible
- Consistent Url after post back
- Adding own rewrite rule providers possible
- Redirects (also permanent) to other domains or websites possible
- Support for Cookie less Sessions
- Adding rewrite rules on runtime
- Very easy installation and use
Installation of UrlRewritingNet.UrlRewrite:
UrlRewritingNet.UrlRewrite is running on each Web server which is running ASP.NET 2.0. To install UrlRewritingNet.UrlRewrite you have to follow these easy steps:
I : Putting UrlRewritingNet.UrlRewrite Dll in the bin folder of your application.
II: Setting UrlRewritingNet.UrlRewrite in web.config File. I am using short brief of how you can set it in Web.config file. You can see detail about using in its documentation file. see below the short briefing of this component. see the bold area, that is related with UrlRewritingNet.UrlRewrite Component.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="urlrewritingnet" restartOnExternalChanges="true" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"/>
</configSections>
<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage="default.aspx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
<rewrites>
<add name="Rule1" virtualUrl="^~/(.*)/web_links.aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/web_links.aspx?id=$1" ignoreCase="true"/>
</rewrites>
</urlrewritingnet>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true">
<assemblies>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
<pages>
<namespaces>
<clear/>
<add namespace="System"/>
<add namespace="System.Collections"/>
<add namespace="System.Collections.Specialized"/>
<add namespace="System.Configuration"/>
<add namespace="System.Text"/>
<add namespace="System.Text.RegularExpressions"/>
<add namespace="System.Web"/>
<add namespace="System.Web.Caching"/>
<add namespace="System.Web.SessionState"/>
<add namespace="System.Web.Security"/>
<add namespace="System.Web.Profile"/>
<add namespace="System.Web.UI"/>
<add namespace="System.Web.UI.WebControls"/>
<add namespace="System.Web.UI.WebControls.WebParts"/>
<add namespace="System.Web.UI.HtmlControls"/>
</namespaces>
</pages>
<httpModules>
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
</httpModules>
<authentication mode="Window" / >
<customErrors mode="Off" defaultRedirect="p_error.aspx">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
</system.web>
</configuration>
After configuration you can set your own rules between <rewrites> </rewrites> tag.
For example : i want to rewrite a url http://www.abc.com/default.aspx?id=30
to
http://www.abc.com/30/default.aspx
you can just add a rule for default.aspx between <rewrites></rewrites> tag.
<rewrites>
<add name="Rule1" virtualUrl="^~/(.*)/default.aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/default.aspx?id=$1" ignoreCase="true"/>
</rewrites>
That's it , after that you can adjust your hyperlink according to that.
For example:
You will use <a href="30/default.aspx">click here</a> instead of <a href="default.aspx?id=30">click here</a>
For detail information see its documentation.