当我使用 Azure 模拟器检查 Request.Url.ToString() 时,MVC 报告不同的端口号

发布于 2024-12-20 11:29:57 字数 583 浏览 0 评论 0原文

我从以下网页开始:

http://127.0.0.1:84/Administration/Accounts/Home

然后选择一个链接,它会带我到:

http://127.0.0.1:84/Administration/Accounts/ShowSummary?ds=0001

在 ShowSummary razor 页面的开头,我检查

Request.Url              http://127.0.0.1:86/Administration/Accounts/ShowSummary?ds=0001}
Request.Url.Authority   "127.0.0.1:86"
Request.UrlReferrer - {http://127.0.0.1:84/Administration/Accounts/Home}

每次尝试此操作时,它总是报告端口 86 而不是端口 84。 这很重要当我存储 URL 时给我。有谁知道为什么它显示不同的端口?正确的端口应该是 84,但 Url 和 Url.Authority 报告为 86

I start off on the following web page:

http://127.0.0.1:84/Administration/Accounts/Home

I then select a link and it takes me to:

http://127.0.0.1:84/Administration/Accounts/ShowSummary?ds=0001

At the start of the ShowSummary razor page I check the

Request.Url              http://127.0.0.1:86/Administration/Accounts/ShowSummary?ds=0001}
Request.Url.Authority   "127.0.0.1:86"
Request.UrlReferrer - {http://127.0.0.1:84/Administration/Accounts/Home}

Every time I try this it always reports the port 86 and not port 84. This is important to me as I store the URL. Does anyone have an idea why it shows a different port? The correct port should be 84 but Url and Url.Authority report 86

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌吟 2024-12-27 11:29:57

是的,我很清楚为什么会发生这种情况。首先,请让我分享有关 Windows Azure 的详细信息,以便您能够了解问题的本质。

当您在 Windows Azure 环境中部署云服务时,它位于负载均衡器(或 LB)后面的虚拟机(所谓的实例)中。当您定义输入端点时,您指示 Windows Azure 基础结构您想要特定的要路由到您的实例的 TCP 端口。因此,LB 在其防火墙端口 80 上打开,并将流量从该端口路由到您的虚拟机。您的虚拟机上的端口 80 也在防火墙中打开。但是,有 2 个不同的主机 - LB 和 VM,您可以在这两个主机上使用端口 80。

为了在本地正确模拟所有这些行为,Windows Azure 模拟器在本地 IIS 中创建一个网站并将其绑定到某个端口(在您的情况下为 86!)。然后它“模拟”LB 并打开端口 84(用于模拟的 LB)。之所以如此,是因为您的角色中可能定义了多个实例。本地模拟器为您定义的每个实例创建一个单独的网站。每个实例都有自己的指定端口(86、87、88 等),而原始端口(在您的情况下为 84)只有一个 - 用于模拟 LB。

这就是为什么您的 Request.Url.Authority 是端口 86 - 因为您的本地站点绑定到端口 86。这就是为什么 URLReferer 是端口 84 - 因为实际请求来自在端口 84 上工作的模拟负载均衡器。

我希望您我猜测这在真实的 Azure 环境中不会发生,因为两个端口都是 80。

当人们处理端口时,我通常建议使用 IPEndpoint 实例,可以从以下位置获取:

RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpIn"].IPEndpoint

但你的情况是有点不同。您正在记录完整的 URL。我不知道 ru 想要实现什么,但这里有一些提示,您可能会认为有价值:

  • 尝试剖析 URL 并使用 IPEndpoint 更改端口
    实例
  • 仅记录路径,而不是完整的 URL(跳过地址和端口)
    部分)

希望这有帮助。

Yes, I have pretty good idea why this is happening. First please let me share a detail on Windows Azure, so you will be able to understand the nature of the issue you have.

When you deploy your cloud service in Windows Azure environment it lives in a Virtual Machine (so called Instance) behind a Load Balancer (or LB). When you define an Input Endpoint, you instruct the Windows Azure infrastructure that you want a specific TCP port to be routed to your Instance. So, the LB opens on its firewall port 80, and routes the traffic from that port to your VM. A port 80 on your VM is also opened in the Firewall. However there are 2 distinguished hosts - the LB and the VM, where you can have port 80 on both.

In order to correctly simulate all that behaviour locally, Windows Azure Emulator creates a Web Site in your local IIS and binds it to some port (86 in your case!). Then it "simulates" the LB and opens port 84 (for the simulated LB). This is so, because you may have more than one instances defined in your role. The local emulator creates a separate website for each and any instance you have defined. And each instance will have its own designated port (86,87,88 etc..), while the original (in your case 84) will be only one - for the simulated LB.

That's why your Request.Url.Authority is port 86 - because your local site is bound to port 86. And that's why the URLReferer is port 84 - becase the actual request comes from the simulated Load Balancer which works on port 84.

I hope you are guess that this will not happen in real Azure environment, because both ports will be 80.

When people deal with port I usually advise to use the IPEndpoint instance which can be obtained from:

RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpIn"].IPEndpoint

But your case is a bit different. You are logging the full URL. I don't know what r u trying to achieve, but here are some hints, you may consider being valuable:

  • Try disecting the URL and change the port using the IPEndpoint
    instance
  • Log just the path, not the full URL (skip the address & port
    parts)

Hope this helps.

心碎无痕… 2024-12-27 11:29:57

如果有些人在本地 Azure 环境中工作时仍然需要获取绝对 Uri,解决方案是:

var absoluteUriString = string.Format(
   "{0}://{1}{2}",
   Request.Url.Scheme,      // protocol
   Request.Headers["Host"], // needed host with correct port number
   Request.RawUrl);         // right part of the uri string
var absoluteUri = new Uri(absoluteUriString);

If some still needs to get absolute Uri while working in the local Azure environment the solution is:

var absoluteUriString = string.Format(
   "{0}://{1}{2}",
   Request.Url.Scheme,      // protocol
   Request.Headers["Host"], // needed host with correct port number
   Request.RawUrl);         // right part of the uri string
var absoluteUri = new Uri(absoluteUriString);
谜泪 2024-12-27 11:29:57

您知道当您在关闭上一个会话后过快重新启动网站时,它会为您分配下一个端口号吗? 84 -> 85 -> 86

在这种情况下,我有时会观察到旧端口号继续工作,即使它们的调试会话已停止。我从来没有真正研究过它,也许这只是我的大脑故障,但也许这就是正在发生的事情。你可以直接尝试86端口,看看是否有效。

You know how when you restart your web site too quickly after closing the previous session, then it will allocate you the next port number? 84 -> 85 -> 86

In this situation I have sometimes observed old port numbers continuing to work, even though their debugging sessions has stopped. I have never really looked into it, and perhaps it is just a brain fault on my part, but perhaps this is what is happening. You could try port 86 directly, and see if that works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文