Click here to Skip to main content
6,630,586 members and growing! (17,422 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate

DotNetNuke URL Rewriting HTTP Module

By onetimesolutions

Always maps your static links to the appropriate Tab ID.
C#, Windows, .NET 1.1, ASP.NET, VS.NET2003, Dev
Posted:3 May 2004
Views:48,604
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.97 Rating: 2.82 out of 5
2 votes, 40.0%
1

2

3
1 vote, 20.0%
4
2 votes, 40.0%
5

Introduction

My intention is not for this to be a massive how-to on HTTP Modules - I think this little DLL is quite useful (I know that there are similar libraries out there that does mostly the same). Hopefully, posting it on Code Project will get it out to as many people as possible.

Breakdown

The Rewrite class implements the IHttpModule interface. This interface is bound to the ASP.NET app in the httpModules section of the System.Web section in the Web.Config.

Add this line to your <system.web> section of your Web.Config file:

<httpModules>
 <add type="Rewriter.Rewrite, Rewriter" name="Rewriter" />
</httpModules>

The interface is implemented as follows:

 public class Rewrite : System.Web.IHttpModule 
 {

The Init function of the interface wires up a BeginRequest event handler. This ensures that as the page is requested, our custom handler intercepts the call, allowing us to replace the path with a new path.

  public void Init(System.Web.HttpApplication Appl) 
  {
    Appl.BeginRequest+=new System.EventHandler(Rewrite_BeginRequest);
  }

The Dispose function is required by the interface.

  public void Dispose() 
  {
    // Empty

  }

The BeginRequest handler sequence receives the HttpApplication context through the sender object.

  public void Rewrite_BeginRequest(object sender, System.EventArgs args) 
  {
    System.Web.HttpApplication Appl = (System.Web.HttpApplication)sender;
    string path = Appl.Request.Path;
    string indicator = "/Content/";
    
    ... More Code ...

  }

This gives the path that we can then process using the GetTab function. This function queries the DotNetNuke TabController system to give us the correct Tab ID and subsequent path, given an original path. The indicator I use is a directory string "/Content/" to indicate that this is different from a normal query.

  private string GetTab(string path, string indicator)
  {
    ...
  }

The SendToNewUrl initiates a rewrite of the path. There are two mechanisms that are possible:

  • The Context.RewritePath retains the URL that it is passed... E.g.: http://www.codeproject.com/Content/ExampleContent.aspx.
  • whereas the Response.Redirect drops the URL and redirects to the appropriate tab... E.g.: http://www.codeproject.com/Default.Aspx?tabid=5.
  public void SendToNewUrl(string url, bool spider, 
                                  System.Web.HttpApplication Appl) 
  {
    if (spider) 
    {
        Appl.Context.RewritePath(url);
    }
    else 
    {
        Appl.Response.Redirect(url, true);
    }
  }

Fingers crossed, you guys get the gist of the system. This is not a difficult task, but hopefully will make your DotNetNuke sites a little easier for navigation or spidering.

Review the GetTab function for more in depth string passing. I know I should have used regular expressions to get to the guts of it all..

There are plenty of additional ways to improve this functionality.

Finally, a big thank you goes out to the Code Project and DotNetNuke and their respective communities.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

onetimesolutions


Member

Occupation: Web Developer
Location: Australia Australia

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralMy vote of 1 Pinmemberyrbyogi19:37 31 Mar '09  
GeneralGood PinmemberMember 435287521:01 8 Sep '08  
Generalpost back PinmemberJacee6:37 11 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 May 2004
Editor: Smitha Vijayan
Copyright 2004 by onetimesolutions
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project