1

I have an ascx control which contains in markup:

<a href="<%# GetName() %>">

In the ascx.cs codebehind file I have the following:

protected string GetName() {
    return HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
}

In codebehind there's also a handler for a button, that calls this method:

protected void myButtonClick(object sender, EventArgs e) 
{
    string server = GetName();
    ...
}

When I request the page containing the control like https://my.site.com/test.aspx, the page shows me the external name (my.site.com), but when I hit the button the call in the handler in codebehind gets the internal name (my123.test.local).

Can anybody explain to me why there's a difference? And how I can get the external domain part in codebehind?

p.s. SERVER_NAME and HTTP_HOST give both the same result.

Also, when I access a page from the internet (https://my.site.com/test.aspx) containing something like:

Response.Write("SERVER_NAME 1: " + Request["SERVER_NAME"]);
Response.Write("SERVER_NAME 2: " + HttpContext.Current.Request.ServerVariables["SERVER_NAME"]);

the first line shows the internal name and the second line shows the external address, like:

SERVER_NAME 1: my123.test.local
SERVER_NAME 2: my.site.com
Jaap
  • 71
  • 1
  • 5
  • This is probably a dumb question but I don't understand what you mean by "internal name". Is that your server's computer name used by windows for networking? Or do you have a reverse proxy set up with your own internal DNS? Or something else? – Mark Rucker Aug 22 '12 at 12:35
  • What's wrong with `Request.Url.Host` – Amiram Korach Aug 22 '12 at 12:35
  • I'm not sure if there's anything wrong with using Request.Url.Host. There seem to be several ways to get the 'internal name' (which is probably not the best description of what I mean). – Jaap Aug 23 '12 at 09:22
  • There seem to be several ways to get the domain name from the request. Some of the ways are Request["SERVER_NAME"], Request["HTTP_HOST"], HttpContext.Current.Request.ServerVariables["SERVER_NAME"], Request.ServerVariables["SERVER_NAME"] (in a control / ascx this Request object is something different than the HttpContext.Current.Request?), Request.Url.Host etc. On my development server and test server these all return the same; the one from the url I use to request the page. But on some production server it goes wrong; I get the internal servername from the Request objects in codebehind... – Jaap Aug 23 '12 at 09:33

0 Answers0