简体   繁体   中英

Url doesn't change using Server.Transfer within an HttpModule

I've got an HttpModule that cleans up hyphens in urls in the BeginRequest , so that I can have a script file called MyFile.asp , but let it be invoked with an endpoint like /my-file.asp . (Yes, it's Classic ASP, not yet migrated to .NET.)

The code looks like this:

'if not an asp file, skip it
If Not Request.Path.EndsWith(".asp") Then Return

'if the physical file exists, bail out, to let that file service the request
If ("~" & Request.RawUrl).ToServerFileObject.Exists Then Return

'if a dehyphenated version of the file exists
If ("~" & Request.RawUrl.RemoveHyphens).ToServerFileObject.Exists Then
    'transfer to that file
    application.Server.TransferRequest("~" & Request.RawUrl.RemoveHyphens, True)
    Return
End If

When I run this and request myfile.asp , the code sees that the file exists on disk, returns, and IIS uses it to service the request. When I request my-file.asp , I was expecting that it would see that the file does NOT exist, and then see that myfile.asp does exist on disk, and transfer to it.

But what happens instead is that this handler just keeps getting called over and over, and each time, Request.RawUrl is set to my-file.asp . It's like the transfer is working, but not updating the request, and this doesn't make any sense to me.

It looks like Request.RawUrl always keeps the value that was originally requested, even after the transfer, but there are other properties, such as Request.AppRelativeCurrentExecutionFilePath or Request.FilePath that do show the url that was transferred to , so I switched to using one of them and I can do what I need now. Thanks to everyone who weighed in with suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM