views:

106

answers:

3

Hi,

I posted a query for 301-redirect using ASP.NET 3.5 here:

http://stackoverflow.com/questions/3609763/redirecting-default-aspx-to-root-virtual-directory

Based on the replies I got there, I realized there might be a bug in ASP.NET's Request.RawUrl method which is unable to return the actual raw url (without /default.aspx) when being used in a sub-directory, i.e. the /default.aspx page is inside a subdirectory.

Can someone please shed some light on this possible bug?

Thanks,

Asif

A: 

Have you looked at the IIS settings for your virtual directory? If there is a default document set to default.aspx then this will explain the infinite loop that you are experiencing. You are telling the website to redirect to the virtual directory without the "default.aspx" and IIS is detecting this on the next request and putting it back in ad infinitum.

Right click your virtual directory, select Properties and then the Documents tab. If default.aspx is in the list then that is what you will get. The Url of the request will be passed to the ASP.NET worker process as /folder/default.aspx rather than /folder/

This is not a bug. If IIS didn't do this, you would get a page not found error.

Sounds to me like you need to investigate URL rewriting: http://msdn.microsoft.com/en-us/library/ms972974.aspx

Daniel Dyson
Thanks for your reply Daniel, but i am a bit confused. If that is the reason for the infinite redirect, then why doesnt this happen when there is no virtual directory and I publish the project at root level (as a website in IIS instead of a sub-directory). Even that website has a default.aspx set, still the above 301-redirect code to remove default.aspx works exactly as expected. Any ideas?
AsifQadri
Hmmm. You got me there. I will have to do some more digging.
Daniel Dyson
@Asif - The reason it doesn't happen at the root level, at least using Cassini, is the RawUrl has a question mark appended after the redirect.
kbrimington
That is a very good point. I didn't see that, @kbrimington. Thanks. I would suggest you add this as an anwser in its own right.
Daniel Dyson
+1  A: 

If you suspect this is a bug, then the place to go is Microsoft Connect, where you can report and discuss the bug directly with Microsoft.

Edit: I was able to reproduce the look per your comments.

I was unable to reproduce the infinite loop, however. I injected code into the Global.asax Application_BeginRequest handler of a web application and got the expected behavior of a single redirect.

There are other, and IMO much better, options for handling global redirect rules. On IIS7, I use the URL Rewrite module to configure rewrite rules in IIS. You can read more about it and download it here: http://www.iis.net/download/urlrewrite. The appeal of a solution such as this is that you can customize and update your rewrite rules without recompiling the application.

Edit: I was able to retrieve the raw URL without the default.aspx (after the redirect) by using instead:

Request.ServerVariables["CACHE_URL"]

It's worth a shot.

kbrimington
I know about the rewrite module, but I cannot use them for other reasons. Did you test it under a subdirectory (virtual directory)? Simply publish your project as a Virtual directory with a default.aspx inside it (lets say site.com/app/) and you should be able to implement 301-redirect to site.com/app when someone types site.com/app/default.aspx. If thats working for you, please let me know. ( iam using asp.net 3.5 and iis7). thanks
AsifQadri
@Asif - Noted, and updated.
kbrimington
+2  A: 

i found a good explanation here

http://codeasp.net/blogs/vivek_iit/microsoft-net/873/301-redirect-from-default-aspx-to-site-root

Thanks

AsifQadri