Click here to Skip to main content
Licence CPOL
First Posted 11 Jan 2006
Views 27,364
Bookmarked 25 times

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

By | 11 Jan 2006 | Article
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:

<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.

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:

<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)

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
GeneralFor Simplest Way of writing URL Rewriting PinmemberDotNetGuts10:40 24 Jul '08  
Generalnot work Pinmemberweb-dv6:47 21 May '07  
GeneralBetter solution PinmemberTerrence Sheflin8:20 3 Apr '07  
Newszdcczxczxczxc PinmemberBaoJFK21:15 25 Jan '07  
QuestionIIS Pinmemberosvaldosalas5:49 13 Oct '06  
QuestionWhat is attrribute? PinmemberRein_Petersen5:52 10 Mar '06  
Answeroops, it's an html editor... PinmemberRein_Petersen5:54 10 Mar '06  
QuestionMapping ext to .net ISAPI in IIS? PinmemberMongris1:09 12 Jan '06  
AnswerRe: Mapping ext to .net ISAPI in IIS? PinmemberJeff.Wang14:11 12 Jan '06  

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.120517.1 | Last Updated 12 Jan 2006
Article Copyright 2006 by Jeff.Wang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid