i want to url mapping from database like dynamically. For Example: I have,
and i want to ,
i want to url mapping from database like dynamically. For Example: I have,
and i want to ,
This solution is pretty intense, but a valid one nevertheless.
We had the same issue in my work some years ago (before we moved to ASP.NET MVC, which I strongly recommend).
First, we made a a new ASP.NET Module (and registered it on IIS or the Web.config). This module would receive the incoming client's requests.
We built our own HttpModule
and made it work along a configuration file, where we defined our valid routes, in your case http://www.example.com/Men/
.
We would then have a list of managers. These managers would work as a pipeline. Each manager would receive the output of the previous one.
Following this approach, our first manager was our RewriteManager
, which handled the rewrite of the incoming request's URL, so future managers (and legacy ones) could continue on using our aspx URLs.
Here is an example of one of our route in our module's configuration file:
<configuration name="note"
mode="1"
urlPattern="^https?://(([a-z0-9\-]*\.)?localhost(:[0-9]+)?/((?'note_id'[0-9]+)-?(?'title'.*))$"
rewriteUrl="/note.aspx?note_id={note_id}&site={ContextInfo.site}">
<manager type="LaNacion.Framework.Web.Managers.RewriteManager, LaNacion.Framework.Web.Managers"/>
<!-- THESE MANAGERS NEED THE ASPX URL, THEY ARE LEGACY MANAGERS -->
<manager type="LaNacion.Framework.Web.Managers.FileCacheManager, LaNacion.Framework.Web.Managers"/>
<manager type="LaNacion.Hola.Web.Managers.NotaManager, LaNacion.Hola.Web.Managers"/>
<manager type="LaNacion.Framework.Web.Managers.OutputImageCacheManager, LaNacion.Framework.Web.Managers"/>
</configuration>
As you can see, we define our route in a regular expression, and recollect required information using named groups on the regex. We later build our legacy URL, using that information we recollected.
In you case, the urlPattern
attribute would look like:
https?://(([a-z0-9\-]*\.)?localhost(:[0-9]+)?/(?'entity'[a-zA-Z]+)
And the rewriteUrl
attribute wourld be:
/Category.aspx?cid=c001&cname={entity}
We do the rewriting of the URL, using the RewritePath
method of the HttpContext
instance:
System.Web.HttpContext.Current.RewritePath(newUrl);
The future managers will then be able to extract query parameters as they have always done, making legacy managers work the same way as before.
I hope I made myself clear enough, and this helped you.
The solution described above by @Mati-Cicero works well if you already know your rewrite rules. If you want to go this way I would also suggest the article at http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net (an old but good one).
But if you want to rewrite to URLs stored in the database, this is what I suggest:
Create a HttpModule this way:
Add it to your
web.config
file like this:And in your rewriter cs file you should have something similar to: