How to differentiate between / (the root) and /default.aspx in ASP.NET
Request.RawUrl always returns the /default.aspx variant. I did not find any way at all to differentiate between these urls. Does anybody know how to do that? Environment is .NET 3.5SP1 o开发者_如何学编程n IIS 7.
I actually had to combat this same problem when designing my URL Rewriter. It has to do with the processes that occur before you can even access the URL's. To get around this you have to make sure that in IIS 7 the default page handling is turned off. Because if there is no default page handling it is not going to go through the extra step of trying to map it to the drive, so you will be the exact URL requested. But this may or may not be an option depending on if you are using System.Web.Routing
or not.
To turn off the default page handling you need to do the following:
- Go to your site in IIS
- Go to Default Document
- Click Disable in the top right corner.
Or you can add the following to your web.config
:
<system.webServer>
<!-- ... other tags here ... -->
<defaultDocument enabled="false" />
</system.webServer>
After you do this the default document will be no longer added to your URL. However be warned that since this is no longer active you cannot rely on default.aspx actually mapping to your directories, you will have to handle this manually or use something like System.Web.Routing to handle this functionality.
To accomplish the same in IIS 6 you need to turn on wildcards:
The following instructions apply for IIS 6.
- Open IIS and right-click on the website and select 'properties'.
- Click the 'Configuration' button under Application Settings section
- Click the 'Insert...' button to create a new wildcard mapping
- Set the executable textbox to aspnet_isapi.dll file location. for .net 2.0, 3.0, 3.5: C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
- Make sure the checkbox 'Verify that file exists' is not checked.
- Press 'OK' to confirm and close all the windows.
NOTE: by the way all the source is available on the site I linked above incase you were curious how I was doing things.
精彩评论