Click here to Skip to main content
15,885,365 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
11 Jan 2006CPOL1 min read 38.4K   26   9
A patch to URL Rewrite in ASP.NET 2.0: Handling directory access.

Introduction

Richard Birkby has written a perfect article on URL Rewriting with ASP.NET. This article is just a patch to it for 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 like: "http://www.apache.org/Book/5.html". But it can't handle URLs like these:

  • 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:

XML
<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, 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 recognize 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.

C#
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 in Web.config:

XML
<httpModules>
    <add name="URLChecking" type="URLCheckingModule"/>
</httpModules>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts24-Jul-08 10:40
DotNetGuts24-Jul-08 10:40 
Generalnot work Pin
web-dv21-May-07 6:47
web-dv21-May-07 6:47 
GeneralBetter solution Pin
Terrence Sheflin3-Apr-07 8:20
Terrence Sheflin3-Apr-07 8:20 
I have written a solution that actually works.

http://www.codeproject.com/useritems/httpmoduledirectoryhandle.asp
Newszdcczxczxczxc Pin
BaoJFK25-Jan-07 21:15
BaoJFK25-Jan-07 21:15 
QuestionIIS Pin
osvaldosalas13-Oct-06 5:49
osvaldosalas13-Oct-06 5:49 
QuestionWhat is attrribute? Pin
Rein_Petersen10-Mar-06 5:52
Rein_Petersen10-Mar-06 5:52 
Answeroops, it's an html editor... Pin
Rein_Petersen10-Mar-06 5:54
Rein_Petersen10-Mar-06 5:54 
QuestionMapping ext to .net ISAPI in IIS? Pin
Mongris12-Jan-06 1:09
Mongris12-Jan-06 1:09 
AnswerRe: Mapping ext to .net ISAPI in IIS? Pin
Jeff.Wang12-Jan-06 14:11
Jeff.Wang12-Jan-06 14:11 

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

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