Reputation: 9427
I'm developing an ASP.NET 3.5 application with Visual Studio 2008.
My default page has some redirection code in the Page_Load method:
protected void Page_Load(object sender, EventArgs e)
{
string sname = Request.ServerVariables["SERVER_NAME"].ToLower();
if (sname.ToLower().Contains("intranet"))
{
Response.Redirect("/intranet/Default.aspx");
}
else if ((sname.ToLower().Contains("extranet")))
{
Response.Redirect("/extranet/Default.aspx");
}
else {
Response.Redirect("/web/Default.aspx");
}
}
I've modified my hosts file so that intranet and extranet redirect to my local machine.
127.0.0.1 intranet
127.0.0.1 extranet
I then type the URL http://extranet in my browser.
However, the problem is that the server variable value returned from Request.ServerVariables["SERVER_NAME"] is always "localhost" and not "extranet"
Any help on how to get the right value?
Many thanks
Upvotes: 12
Views: 31961
Reputation: 131
Youre right You want to retrieve the full address of the website that the request came to. Do not use "SERVER_NAME", use "HTTP_HOST". Read here, http://www.requestservervariables.com/get-address-for-website
Upvotes: 5
Reputation: 9427
Request.ServerVariables["HTTP_HOST"] gets the value I was looking for :)
Upvotes: 13
Reputation: 11
Your host files only redirect the requests to a specific IP address - you cannot change the requesting machines name by editing them.
Upvotes: 0
Reputation: 39413
Server_Name
returns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs
Why don't you use Request.URL
?
Upvotes: 0