C# Request.Url.Host 与 Request.Url.Authority

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18303334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:51:23  来源:igfitidea点击:

Request.Url.Host vs Request.Url.Authority

c#asp.net

提问by rwkiii

I've inherited an ASP.NET web application written in C#. In many pages throughout the site the hostname is retrieved using:

我继承了一个用 C# 编写的 ASP.NET Web 应用程序。在整个站点的许多页面中,使用以下方法检索主机名:

BaseHost = Request.Url.Host;

Since I am using Visual Studio 2012 Express and it's installed local IIS Express server, I seem to be stuck with a port number appended to the hostname (localhost) when I debug/run locally. The above code does not result in the port number being included and as such breaks links that are generated by code (menu item links, redirects, etc).

由于我使用的是 Visual Studio 2012 Express 并且它安装了本地 IIS Express 服务器,因此当我在本地调试/运行时,我似乎被附加到主机名 (localhost) 的端口号所困扰。上面的代码不会导致包含端口号,因此会中断由代码生成的链接(菜单项链接、重定向等)。

I see that I can overcome the issue by changing the code to:

我发现我可以通过将代码更改为:

BaseHost = Request.Url.Authority;

This seems to fix it by including the port when I'm running locally (localhost:4652) and when published to my staging server (development.mysite.com).

这似乎通过在我本地运行 (localhost:4652) 和发布到我的登台服务器 (development.mysite.com) 时包含端口来解决它。

My question: Is this bad form? Is there a time or a situation in which this is going to cause problems on my live site? It just seems a lot easier to do a quick replace of all these instances. I've considered writing a small routine to append : with Request.Url.Port, but it seems easier just to use Request.Url.Authority. Too easy maybe...

我的问题:这是不好的形式吗?是否有时间或情况会导致我的实时站点出现问题?快速替换所有这些实例似乎要容易得多。我考虑过编写一个小程序来 append : with Request.Url.Port,但似乎更容易使用Request.Url.Authority. 可能太容易了...

I've tried to research my question online and at MSDN, but I don't see an answer.

我试图在网上和 MSDN 上研究我的问题,但我没有看到答案。

采纳答案by Yahia

According to MSDNAuthorityincludes the port number while Hostdoes not. Another aspect is that Authoritywill escape reserved characters if need be.

根据MSDNAuthority包括端口号而Host没有。另一个方面是,Authority如果需要,将转义保留字符。

Without knowing your application it is hard to say whether it is a good idea, but in general I would suspect that it won't break anything... so go ahead...

在不知道您的应用程序的情况下,很难说这是否是一个好主意,但总的来说,我怀疑它不会破坏任何东西......所以继续......

Another option is to run the application IIS instead of IIS Express...

另一种选择是运行应用程序 IIS 而不是 IIS Express ...

回答by HBlackorby

My problem with this is that it ALWAYS adds the port, even when the port is not required. This can cause issues with multiple servers in some cases For example, in a production server environment behind a firewall on a pair of load-balanced web servers, it kept putting the firewall port in place, but that caused the URL to break because the port was tied to a specific web server in the server farm that wouldn't map correctly through the firewall. So I would be very careful with this method if you're using it across multiple servers. It caused a breaking issue with our application and had to be reverted back to using Url.Host. Plus, it made production web URL's look weird with the port number.

我的问题是它总是添加端口,即使不需要端口。在某些情况下,这可能会导致多个服务器出现问题。例如,在一对负载平衡的 Web 服务器上的防火墙后面的生产服务器环境中,它一直将防火墙端口放在适当的位置,但这会导致 URL 中断,因为端口绑定到服务器群中的特定 Web 服务器,该服务器无法通过防火墙正确映射。因此,如果您在多个服务器上使用这种方法,我会非常小心。它导致我们的应用程序出现重大问题,必须恢复使用 Url.Host。另外,它使生产网址的端口号看起来很奇怪。