0

I have an aspx file Content.aspx. When you visit content.aspx/FAQ it uses the value in Request.PathInfo to determine which content to load/display from the database (in this case, the FAQ data).

This works perfectly.

Until you pass /Content.aspx/FAQ to Server.Execute(). Then it throws an Exception that an error occurred executing the Handler for the page. When I dig into the InnerException, I see the message The file '/Content.aspx/FAQ' does not exist. This leads me to believe that Execute() is not handling PathInfo correctly and is instead treating as an actual part of the path on disk.

Is there a way to get the Execute() method to properly handle these PathInfo parameters? Unfortunately it's too late now to change the way this application is doing this as this kind of 'parameter passing' is used throughout in lieu of traditional query strings for this purpose while query strings are used mostly for more complex parameters or in combination with PathInfo modifiers.

DavidScherer
  • 863
  • 1
  • 14
  • 26
  • Server.Execute expects a valid URL as parameter, /Content.aspx/FAQ is not a valid URL in your site, why don't you use /Content.aspx?Section=FAQ) ??? Check this: https://stackoverflow.com/questions/9261427/server-transfer-vs-server-execute and this https://learn.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.execute?view=netframework-4.7.2 – JCM Feb 06 '19 at 16:05
  • @JCM While I appreciate your feedback, that was kinda established already above. Additionally, I've pointed out it is too late to change the fact that PathInfo is used heavily across this application. Further, per usual, the MS documentation is not clear that the path is required to be 'valid' in this manner, which is further confused by their support of the PathInfo property elsewhere. – DavidScherer Feb 06 '19 at 16:15
  • Maybe the better question is what is the convoluted way MS expects us to handle Server Paths which use PathInfo with Server.Execute(). – DavidScherer Feb 06 '19 at 16:16
  • May you use Response.Redirect instead of Server.Execute? – JCM Feb 06 '19 at 16:17
  • Unfortunately I don't think that's a reasonable solution since I'm doing this within an `IHttpHandler` with the goal of supporting a client's request that we make pages printable as PDFs. So I'm needing to get the HTML for pages in a fairly central way to the application with the end goal of passing said HTML to a HTML to PDF converter such as HiQPDF or PrinceXML. – DavidScherer Feb 06 '19 at 16:20

1 Answers1

0

So I found a way to resolve this, albeit it is a touch convoluted.

In my MakePagePdfHandler.vb which implements IHttpHandler (not sure how important this is, so including it for clarity), I check that the path exists, and if it does not, I know there is a PathInfo part attached. From there I can redirect back to my handler with the PathInfo attached to the handler, then we can check again for File.Exists() and compare the PathInfo for the request along with the detected path info in the URI we are trying to Server.Execute() and if they match, remove the path info from the URI to execute and the path info on the hander will "fall through" or "trickle down" as if it were present like normal.

Dim pathInfo as String

If Not File.Exists(Context.Server.MapPath(path)) Then
    Dim parts = link.Split("/") ' link contains the URI to Execute
    pathInfo = "/" + parts(parts.length - 1)

    If context.Request.PathInfo <> pathInfo Then
        context.Response.Redirect(String.Format("{0}{1}?{2}", context.Request.Path, pathInfo, context.Request.QueryString))
        Exit Sub
    End If
End If

If pathInfo IsNot Nothing Then
    link = link.replace(pathInfo, "")
End If

context.Server.Execute(link, strWriter)
DavidScherer
  • 863
  • 1
  • 14
  • 26