views:

739

answers:

6

I have the following example page structure:

  • Webpage.aspx
  • Script.aspx

If I call Server.Execute("Script.aspx") from Webpage.aspx, how can I detect in Script.aspx that it was called from Webpage.aspx and not directly from a web browser?

I've tried checking the Referrer but this only seems to return domain and not the script.

I'm using ASP.NET Web Forms on .NET 3.5

A: 

At least you can tell if the request is local through Request.IsLocal .

baretta
That only returns true if the browser request was from the local server. In my case, it always returns false.
GateKiller
in that case, i really don't think there are any references back to the original page. Your best bet is probably as others have said, to pass this information in the querystring
baretta
A: 

You could pass a querystring parameter to Script.aspx (which will help you identify that it was called from WebPage.aspx).

Server.Execute("script.aspx?xFrom=webPage.aspx")

EDIT: I think the Request should have some value in it for you to know that it is running webpage.aspx.

EDIT2: Request.Url?

shahkalpesh
+12  A: 

Since Server.Execute runs the new page with the same context as the original page, all the properties of Request should still reflect the original request to Webpage.aspx (except for CurrentExecutionFilePath, which hopefully contains "/Script.aspx"). Request.Path should contain "/Webpage.aspx", while Request.Url will give the full Uri object if you need to see the domain or querystring.

You can also add values to Context.Items before calling Server.Execute and read them in Script.aspx

stevemegson
+1 for using Context.Items - that's what it's for.
Sam
+1 for the same reason as Sam gave. like it! :-)
REA_ANDREW
A: 

HttpRequest.FilePath will return the URL to the page requested, which you can then do a substring match on for "Script.aspx".

(Take this answer with a huge grain of salt though, my .Net is pretty poor.)

Ant P.
This does return "Script.aspx" but I want it to return "Webpage.aspx". Thanks for the answer.
GateKiller
Hold on... Try the CGI 'SCRIPT_NAME' variable instead. I haven't got an ASP box to test this out on right now, but hopefully it should work the same as it does in PHP (which does do the right thing in this case).
Ant P.
+2  A: 

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.

Snickers
Thanks. This was the best method suggested because I didn't have to do any extra work outside of script.aspx :)
GateKiller
A: 

Assuming you don't have any IHttpHandler funniness, you can check if HttpContext.PreviousHandler is not null. However, since there is a million (well, several) reasons that PreviousHandler could be set, I'd go with stevemegson's recommendation of using HttpContext.Items before calling Server.Execute.

Richard Szalay