How to detect if an aspx page was called from Server.Execute?

前端 未结 6 1421
醉梦人生
醉梦人生 2021-01-13 13:38

I have the following example page structure:

  • Webpage.aspx
  • Script.aspx

If I call Server.Execute(\"Script.aspx\") from Webpa

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 14:04

    In Script.aspx.cs, you can simply check the Request.Path in comparison to the current execution path.

    if ( Request.CurrentExecutionFilePath == Request.Path ) 
    {
       //This has been called from a web browser
    } else {
       //This has been executed from the file Request.Path
    }
    

    Why?

    Portions of the request are passed on untouched when you call the Server.Execute. Thus, if you were to look at the value of Request.Path from your Script.aspx.cs after using a Server.Execute in your Webpage.aspx.cs, you will see it has a value of "/Webpage.aspx".

    However, if a web browser were to access Script.aspx directly, the value of Request.Path from Script.aspx.cs will result in "/Script.aspx". The currentExecutionPath will always yield the currently executed script, so comparing the two will give the desired result.

    Hope this helps.

提交回复
热议问题