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 ,
On
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:
public class DBRewriteModule : IHttpModule
{
public DBRewriteModule()
{
}
public void Init(HttpApplication context)
{
context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
}
void context_AuthorizeRequest(object sender, EventArgs e)
{
Rewriter rw = new Rewriter();
rw.Process();
}
}
Add it to your web.config file like this:
<system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true"> <add name="DBRewrite" type="smartdev.web.Modules.DBRewriteModule" /> </modules> </system.webServer>
And in your rewriter cs file you should have something similar to:
public class Rewriter
{
public Rewriter()
{
}
public bool Process()
{
// get path from database based on your original path:
// use HttpContext.Current.Request.Path and HttpContext.Current.Request.QueryString
string substPath = "...your db logic here ...";
HttpContext.Current.RewritePath(substPath);
}
}
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
HttpModuleand made it work along a configuration file, where we defined our valid routes, in your casehttp://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:
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
urlPatternattribute would look like:And the
rewriteUrlattribute wourld be:We do the rewriting of the URL, using the
RewritePathmethod of theHttpContextinstance: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.