Request.ServerVariables [“SERVER_NAME”]始终是localhost

时间:2020-12-18 01:53:35

I'm developing an ASP.NET 3.5 application with Visual Studio 2008.

我正在使用Visual Studio 2008开发ASP.NET 3.5应用程序。

My default page has some redirection code in the Page_Load method:

我的默认页面在Page_Load方法中有一些重定向代码:

    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.

我修改了我的hosts文件,以便Intranet和Extranet重定向到我的本地计算机。

127.0.0.1       intranet
127.0.0.1       extranet

I then type the URL http://extranet in my browser.

然后我在浏览器中输入URL http:// extranet。

However, the problem is that the server variable value returned from Request.ServerVariables["SERVER_NAME"] is always "localhost" and not "extranet"

但是,问题是从Request.ServerVariables [“SERVER_NAME”]返回的服务器变量值始终是“localhost”而不是“extranet”

Any help on how to get the right value?

有关如何获得正确价值的任何帮助?

Many thanks

非常感谢

4 个解决方案

#1


13  

Request.ServerVariables["HTTP_HOST"] gets the value I was looking for :)

Request.ServerVariables [“HTTP_HOST”]获取我正在寻找的值:)

#2


5  

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

你是对的你想要检索请求来的网站的完整地址。不要使用“SERVER_NAME”,请使用“HTTP_HOST”。请阅读http://www.requestservervariables.com/get-address-for-website

#3


0  

Server_Name returns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs

Server_Name返回服务器的主机名,DNS别名或IP地址,就像它在自引用URL中显示的那样

Why don't you use Request.URL?

你为什么不使用Request.URL?

#4


0  

Your host files only redirect the requests to a specific IP address - you cannot change the requesting machines name by editing them.

您的主机文件仅将请求重定向到特定的IP地址 - 您无法通过编辑来更改请求的计算机名称。

#1


13  

Request.ServerVariables["HTTP_HOST"] gets the value I was looking for :)

Request.ServerVariables [“HTTP_HOST”]获取我正在寻找的值:)

#2


5  

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

你是对的你想要检索请求来的网站的完整地址。不要使用“SERVER_NAME”,请使用“HTTP_HOST”。请阅读http://www.requestservervariables.com/get-address-for-website

#3


0  

Server_Name returns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs

Server_Name返回服务器的主机名,DNS别名或IP地址,就像它在自引用URL中显示的那样

Why don't you use Request.URL?

你为什么不使用Request.URL?

#4


0  

Your host files only redirect the requests to a specific IP address - you cannot change the requesting machines name by editing them.

您的主机文件仅将请求重定向到特定的IP地址 - 您无法通过编辑来更改请求的计算机名称。