Click here to Skip to main content
Licence 
First Posted 13 Jan 2006
Views 19,974
Bookmarked 32 times

A Simple Way to Handle Directory Access in ASP.NET 2.0

By | 13 Jan 2006 | Article
A patch to the code in URL Rewrite in ASP.NET 2.0: handles directory access.

Introduction

Richard Birkby has written a perfect article: URL Rewriting with ASP.NET. This article is just a patch to it about how to handle directory access.

Problems

Using Richard's framework (it's really cool!), we can hide "http://www.apache.org/BookDetails.pl?id=5" and use a well formed URL "http://www.apache.org/Book/5.html". But it can't handle URLs like this:

  • http://www.apache.org/Book/Java/
  • http://www.apache.org/Book/C#/

Because, ASP.NET will try to resolve this directory and find "default.aspx" in it. Of course, it will return a 404 Not Found Error.

The Simple Way: Step 1

First, we should update the web.config file as follows:

<httpHandlers>
    <add verb="*" path="Default.aspx" type="URLRewriteHandler"/>
    <add verb="*" path="*/" type="URLRewriteHandler"/>
</httpHandlers>

We add a handler which maps to Default.aspx, it will work when we access the "/" of a virtual path. And another handler is mapped to "*/", it will work when we access a directory, e.g., "/some/path/". Now, the URLRewriteHandler can handle directory access which ends with a "/".

The Simple Way: Step 2

Want to see what happens if someone types in "http://www.apache.org/Book/Java"? We still will get a 404 Not Found Error. It means that we should handle URLs which have no extension, but we can't write this mapping in web.config directly because ASP.NEt won't reorganize it.

How should we think about "http://www.apache.org/Book/Java"? Is it a wrong URL? Yes! All we need to do is correct this mistake using the correct URL (which has a "/" at the end of the line). The idea is to write an HTTP module to handle the wrong URL and force the client to redirect to the correct path.

public class URLCheckingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(Context_OnBeginRequest);
    }

    private void Context_OnBeginRequest(object sender, EventArgs e)
    {
        // check Request 
        HttpContext context = HttpContext.Current;
        HttpRequest request = context.Request;
        string file = request.FilePath;
        string ext = Path.GetExtension(file);
        if (string.IsNullOrEmpty (ext) && ! file.EndsWith ("/"))
        {
            string q = request.QueryString.ToString ();
            string path = request.FilePath + "/" + 
                          (string.IsNullOrEmpty(q) ? "" : q) ;
            context.Response.Redirect(path);
        }
    
    }

    public void Dispose()
    {
        
    }
}

Then register it into Web.config:

<httpModules>
    <add name=¡±URLChecking" type="URLCheckingModule"/>
</httpModules>

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

Jeff.Wang

Web Developer

China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionURLRewriteHandler Pinmemberweb-dv5:29 21 May '07  
hello,
thank you!
 
URLRewriteHandler is not work!
what can I put?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 13 Jan 2006
Article Copyright 2006 by Jeff.Wang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid