1

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.

Adam
  • 16,089
  • 6
  • 66
  • 109
Joshua Frank
  • 13,120
  • 11
  • 46
  • 95
  • A server.transfer doesn't change the url but gets the file and returns it to the browser under the same url. You would need to do a Response.Redirect. However since this is classic asp I am not sure of the specifics here. – Adam Jun 18 '14 at 17:08
  • 1
    Transfer doesn't end the current request; it is saying "stop processing this one, and process the new URL", but the client has no idea what's going on and still sees the original requested URL. – Cᴏʀʏ Jun 18 '14 at 17:09
  • @Adam: I *want* it to do that, but I want it to return a different file than the one requested. I don't want to `Redirect` because I want it to be invisible to the user. – Joshua Frank Jun 18 '14 at 17:59
  • @Cory: But that isn't what it's doing. It's transferring, but with the same filename. – Joshua Frank Jun 18 '14 at 18:01
  • So have you tried debugging this code in a debugger? You should be able to put a breakpoint in the beginning and see what each variable contains, following the logic and path of the code is taking. Perhaps one of your extension methods isn't doing what you think it is. – Cᴏʀʏ Jun 18 '14 at 18:10
  • @Cory: I put a breakpoint on that `Server.TransferRequest` line and it hits and the new url is correct, but because the new request is also for a file that matches the module pattern of `*.asp`, it goes back to the same method, but with the original url, and this causes an infinite loop. – Joshua Frank Jun 18 '14 at 18:23
  • Back to Adam's point -- the transfer doesn't change the base properties of the original request. So you're handling the transfer, but the request object is unchanged, I believe. (Hence the behavior you're seeing.) From MSDN: _When you use the Transfer method, the state information for all the built-in objects are included in the transfer. This means that any variables or objects that have been assigned a value in session or application scope are maintained. In addition, all of the current contents for the Request collections are available to the .asp file that is receiving the transfer._ – Bret Jun 18 '14 at 19:43
  • Sorry, ran out of chars -- when you put a breakpoint on the line, were you checking Request.RawUrl, or were you confirming that Request.RawUrl.RemoveHyphens matched what you expected? – Bret Jun 18 '14 at 19:53
  • @Bret: the problem seems to be that `RawUrl` is preserved during a `TransferRequest`, but there are other url properties that do reflect the transfer. See the answer that I just posted. – Joshua Frank Jun 19 '14 at 02:02

1 Answers1

0

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.

Joshua Frank
  • 13,120
  • 11
  • 46
  • 95