How to dynamic url mapping in asp.net with c#?

4.8k views Asked by At
2

There are 2 answers

0
SmartDev 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);
    }
}
0
Matias Cicero On

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}&amp;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.