tags:

views:

817

answers:

4

Branching off from this link it seems that Request.ServerVariables("SCRIPT_NAME") doesn't return anything. I've just set up a simple loop to see if anything comes out at all but with no luck. Any ideas?

For Each s As String In Request.ServerVariables
   Label1.Text += s
Next

ServerVariables brings back a list of Strings in key->value form as defined in System.Collections.Specialized.NameValueCollection

EDIT: It seems to be a problem with getting the ServerVariables from a page using master pages. In my code behind model I'm defining the following:

Public Partial Class Test
    Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Write(Context.Request.ServerVariables("SCRIPT_NAME"))
  End Sub
End Class

This doesn't work, quite simply. If I enter the code within the Page_Load event within the .aspx page, however, by doing the following:

<% Response.Write(Context.Request.ServerVariables("SCRIPT_NAME")) %>

Then it works fine. Any ideas as to why that's happening?

A: 

The definition at http://msdn.microsoft.com/en-us/library/ms524602.aspx says "A virtual path to the script being executed, for example, "/vdir/default.asp". This is used for self-referencing URLs.". I think the ".asp" part may be a hint as to why this isn't working for ASP.NET.

I recommend against ever using Request.ServerVariables. Anything created for use in Classic ASP or ISAPI should make you nervous.

John Saunders
It's still used in *.aspx files, just in a different context, that is, the Http Context.
Kezzer
I know ServerVariables are used, but is Context.Request.ServerVariables("SCRIPT_NAME") still filled in for .ASPX?
John Saunders
Yup, works fine. I can't see what alternative there is anyway?
Kezzer
HttpContext.Current.Request.Url.AbsolutePath. Consider: you're not running a script.
John Saunders
A: 

If you are trying to return the script name, try:

Request.CurrentExecutionFilePath

and if you just wanted the script name without the path, you could use:

Path.GetFileName(Request.CurrentExecutionFilePath)

Path.GetFileName() requires the System.IO namespace

GateKiller
Good call, but getting the path hadn't seemed to be the problem, it was just where it was output (check my edit) :)
Kezzer
A: 

I tried your code, and get the result you want :

<% Response.Write(Context.Request.ServerVariables("SCRIPT_NAME")) %>

Returns : /TESTERS/Default.aspx

You can replace it with this, returns same result :

string path = HttpContext.Current.Request.Url.AbsolutePath;

Returns : /TESTERS/Default.aspx

Canavar
Getting the actual path isn't the problem, it's getting it from the code behind model in the master page that's the problem. It seems to only work if it's within the page unfortunately.
Kezzer
+1  A: 

Are you sure you're not getting this back?

Using Response.Write from the code behind (either in the page or the master page) will output the string at the top of the page (usually), before the rest of the HTML, etc, which is probably not where you're expecting to see it.

I put a literal control on a master page, before the content placeholder:

On Master: <asp:Literal id="LiteralMaster" runat="server"></asp:Literal>

And then setting it's Text property in the master page's Page_Load event to:

LiteralMaster.Text = Context.Request.ServerVariables("SCRIPT_NAME")

I also added a literal control in the content placeholder on a page that used this master page:

On page: <asp:Literal id="LiteralPage" runat="server"></asp:Literal>

And in the page's Page_Load event I had the following:

LiteralPage.Text = Context.Request.ServerVariables("SCRIPT_NAME")

Which resulted in:

On Master: /LittleTest/UsingMaster.aspx
On page: /LittleTest/UsingMaster.aspx

Modifying my page's Page_Load event to become:

LiteralPage.Text = Context.Request.ServerVariables("SCRIPT_NAME")
Response.Write("From response.write: " &
  Context.Request.ServerVariables("Script_Name"))

Resulted in the following display in my browser:

From response.write: /LittleTest/UsingMaster.aspx
On Master: /LittleTest/UsingMaster.aspx
On page: /LittleTest/UsingMaster.aspx

However, the html looked more like:

From response.write:/LittleTest/UsingMaster.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <title></title>
  </head>
  <body>
    <form name="aspnetForm" 
      method="post" action="UsingMaster.aspx" id="aspnetForm">
      <div>
        On Master: /LittleTest/UsingMaster.aspx
        <br />
        On page: /LittleTest/UsingMaster.aspx
      </div>
    </form>
  </body>
</html>

Edit to respond to comment

You are assigning the string representation of a NameValueCollection element to the contents of your label, this is just the NAME, not the combination of the NAME and the VALUE

Try the following:

For Each s As String In Request.ServerVariables
  Label1.Text += s & ": " & Request.ServerVariables(s)
Next

For more info, take a look at the MSDN docs:

HttpRequest.ServerVariables

Zhaph - Ben Duguid
We're very aware of this ;) if you look at the top of my question I actually assign the output to a label which exists on the screen. The other code I wrote was just something I wrote here, it wasn't my actual code - the chances are it wouldn't output as it's using the master page anyway.
Kezzer
See my edit - this code was tested with a page using a masterpage - I was able to output the value of ServerVariables("Script_Name") correctly from the page_load events of both the master page and the page itself.
Zhaph - Ben Duguid