views:

308

answers:

1

I am encountering an unexpected behavior:

The following statement works fine:

Context.RewritePath( "~/Default.aspx" );  // redirect to default doc, explicitly

This gives me a 404 error:

Context.RewritePath( "~/" );  // redirect to default doc, implicitly

Loading document / from a browser without doing any URL rewriting correctly loads the document, so I figure IIS is correctly configured, and that / and /Default.aspx indeed refer to the same document.

I would rather use the latter statement, as there is a possibility that the Default document name will be changed in IIS as time goes on. I'm assuming the solution involves some method to retrieve the Default Document name from IIS, however I've been unable to locate such a method.

So my question is: What is the correct way to specify a default document when rewriting the URL?

+3  A: 

Your problem is that IIS handles the path translations for the "default document" before it turns control over to asp.net.

When a browser requests a URL without a file name, IIS will check the list of "default documents" configured for that site. It then looks for physical files in the requested path that match the name of the configured default documents. It then returns the first matching default document that physically exists on the disk.

After this, if the requested file is an asp.net file, it will invoke the asp.net runtime and hand off processing to asp.net.

Your URL re-writing takes place inside the asp.net process. It has no awareness of IIS's settings with regards to default documents and such. When you use a technique like URL re-writing that take place entirely within asp.net, you can't use default documents and such. So always re-write using the page name.

Stephen M. Redd
+1 for a better articulation of problem I'm experiencing.
Bob Kaufman
My answer is that you can't use default documents in url rewrite. You have to specify the page name. There are some techniques that might allow you to get configuration info from IIS about the default document, but it would not be worth the effort, would fail in restricted trust, and would not be consistent from one IIS version to another.You could use response.redirect which will cause the client browser to request the new URL, and you can use "~/" for that. Depending on what exactly you are trying to achieve by the rewrite, that might be a better solution anyway.
Stephen M. Redd