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?
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:
And then setting it’s Text property in the master page’s Page_Load event to:
I also added a literal control in the content placeholder on a page that used this master page:
And in the page’s Page_Load event I had the following:
Which resulted in:
Modifying my page’s Page_Load event to become:
Resulted in the following display in my browser:
However, the html looked more like:
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 more info, take a look at the MSDN docs: