ASP.NET MVC:如何可靠地获取当前请求的真实URL? [英] ASP.NET MVC: How to reliably get the real URL of the current request?
问题描述
现在我有一个在 http://127.0.0.1:8081/(它实际上在Azure计算模拟器中运行).浏览器位置栏中的端口号清楚地显示为8081.
Right now I have an MVC application running on http://127.0.0.1:8081/ (it is actually running in the Azure compute emulator). The browser location bar clearly says 8081 for the port number.
但是Request.Url
和Request.RawUrl
都给我 http://127.0.0.1:8082/.
我需要发送一封带有URL的电子邮件,因此我需要正确的主机名和端口号.如何可靠地获得实际的真实URL,并且没有这种意外的偏差?
I need to send an e-mail with a URL in it, so I need the correct hostname and port number. How do I get the actual, real URL reliably and without such unexpected deviation?
推荐答案
您可以使用Request.Headers["Host"];
,它将为您提供具有正确端口号的主机名(或IP).然后,您可以重建URL.
You can use Request.Headers["Host"];
which will give you the host name (or IP) with the correct port number. Then you can reconstruct the URL.
这篇关于ASP.NET MVC:如何可靠地获取当前请求的真实URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!