Request.Url.ToString() returns the machine name ne

2019-08-15 06:03发布

When I deployed my application on the server I faced this issue:

Request.Url.ToString(); returns the machine name instead of the domain name.

For example:

Instead of returning http://www.domainName.com/default.aspx it returns http://appserver-01/default.aspx.

Note: Everything was OK before the deployment.

7条回答
Ridiculous、
2楼-- · 2019-08-15 06:16

Host header (which is where Request.Url gets the domain name) is rewritten with the machine that proxies traffic from outside.

Original domain lands in header X-Forwarded-Host. You should read it from there.

You can also force proxy to preserve host header. If your proxy is apache mod_proxy you can use directive:

ProxyPreserveHost On

查看更多
【Aperson】
3楼-- · 2019-08-15 06:19

I has the same issue, I used this code instead:

"http://" + 
Request.ServerVariables["HTTP_HOST"] + 
Request.ServerVariables["URL"]
查看更多
混吃等死
4楼-- · 2019-08-15 06:25

You could also look into the myriad other ways to resolve the url or domain. For example, Request.RawUrl typically returns whatever was typed into the address bar, etc.

查看更多
该账号已被封号
5楼-- · 2019-08-15 06:30

We had a similar issue. In our case it was - what was the very first request that hit the site (after IISReset). If you hit it with this URL: http://appserver-01/default.aspx then it will keep using it.

Try the following: 1. IISReset 2. Request this URL: http://www.domainNmae.com/default.aspx 3. see if the issue is resolved

查看更多
再贱就再见
6楼-- · 2019-08-15 06:41

Well, digging in with Reflector, it seems to me that the Uri object contained in Request.Url is definitely built from the information that comes in from the request headers.

In light of that, I's suspect that maybe the requests coming in aren't what you think they are. Try having a look at the Raw headers in the requests coming into your server. I'd use a packet sniffer for this, I'd bet they reflect the problem you're seeing. If so, the problem isn't on your web server, it's somewhere in front of that.

Do you have any forwarding set up somewhere that could be causing this issue? Like http forwarding done by some sort of domain controller? Do you have any custom HttpHandlers that are massaging the request before handing them off? If you do, I'd look there. If you don't think you do, ask your Network Admin (if you have one) just to be safe.

All of this is a stab in the dark as I don't know your whole set up. But it's my best guess.

Good luck!

查看更多
一夜七次
7楼-- · 2019-08-15 06:42

What value returns this calls?

Request.ServerVariables("SERVER_NAME");
Request.ServerVariables("HTTP_HOST");
查看更多
登录 后发表回答