Click here to Skip to main content
14,931,892 members
Articles / Web Development / ASP.NET
Posted 11 Jan 2006


26 bookmarked

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
A patch to URL Rewrite in ASP.NET 2.0: Handling directory access.


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.


Using Richard's framework (it's really cool!), we can hide "" and use a well formed URL like: "". But it can't handle URLs like these:


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:

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

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 ""? 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 ""? 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) ;

    public void Dispose()

Then register it in Web.config:

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


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


About the Author

Web Developer
China China
No Biography provided

Comments and Discussions

GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts24-Jul-08 10:40
MemberDotNetGuts24-Jul-08 10:40 
Generalnot work Pin
web-dv21-May-07 6:47
Memberweb-dv21-May-07 6:47 
GeneralBetter solution Pin
Terrence Sheflin3-Apr-07 8:20
MemberTerrence Sheflin3-Apr-07 8:20 
Newszdcczxczxczxc Pin
BaoJFK25-Jan-07 21:15
MemberBaoJFK25-Jan-07 21:15 
QuestionIIS Pin
osvaldosalas13-Oct-06 5:49
Memberosvaldosalas13-Oct-06 5:49 
QuestionWhat is attrribute? Pin
Rein_Petersen10-Mar-06 5:52
MemberRein_Petersen10-Mar-06 5:52 
Answeroops, it's an html editor... Pin
Rein_Petersen10-Mar-06 5:54
MemberRein_Petersen10-Mar-06 5:54 
QuestionMapping ext to .net ISAPI in IIS? Pin
Mongris12-Jan-06 1:09
MemberMongris12-Jan-06 1:09 
AnswerRe: Mapping ext to .net ISAPI in IIS? Pin
Jeff.Wang12-Jan-06 14:11
MemberJeff.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.