.NET - 获取协议、主机和端口
.NET中有没有一种简单的方法可以快速获取当前的协议、主机和端口? 例如,如果我访问以下 URL:
http://www.mywebsite.com:80/pages/page1.aspx
,
我需要返回:
http://www. mywebsite.com:80
我知道我可以使用 Request.Url.AbsoluteUri
来获取完整的 URL,并且我知道我可以使用 Request.Url.Authority
获取主机和端口,但我不确定在不解析 URL 字符串的情况下获取协议的最佳方法。
有什么建议么?
Is there a simple way in .NET to quickly get the current protocol, host, and port? For example, if I'm on the following URL:
http://www.mywebsite.com:80/pages/page1.aspx
I need to return:
http://www.mywebsite.com:80
I know I can use Request.Url.AbsoluteUri
to get the complete URL, and I know I can use Request.Url.Authority
to get the host and port, but I'm not sure of the best way to get the protocol without parsing out the URL string.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
与霍尔格的回答非常相似。 如果您需要获取 URL 可以执行以下操作:
Uri 类 提供了一整套方法,其中许多方法我没有列出。
在我的实例中,我需要抓取
LocalHost
以及Port Number
,所以这就是我所做的:成功抓取:
http://localhost:12345
Very similar to Holger's answer. If you need to grab the URL can do something like:
The Uri class provides a whole range of methods, many which I have not listed.
In my instance, I needed to grab
LocalHost
along with thePort Number
, so this is what I did:Which successfully grabbed:
http://localhost:12345
Request.Url 将返回请求的 Uri。 一旦你拥有了它,你就可以检索几乎任何你想要的东西。 要获取协议,请调用 Scheme 属性。
示例:
希望这有帮助。
Request.Url will return you the Uri of the request. Once you have that, you can retrieve pretty much anything you want. To get the protocol, call the Scheme property.
Sample:
Hope this helps.
甚至更短的方式,可能需要更新的 ASP.Net:UriComponents
枚举允许您指定要包含的 URI 的哪个或哪些组件。
Even shorter way, may require newer ASP.Net:
The UriComponents enum lets you specify which component(s) of the URI you want to include.
一种更结构化的方法是使用 UriBuilder。 这避免了直接的字符串操作。
A more structured way to get this is to use UriBuilder. This avoids direct string manipulation.
就我而言,
可以获取
https://www.mywebsite.com:80
In my case
works to get
https://www.mywebsite.com:80
就我而言,我使用这里
return
http://www.mywebsite.com:80 /pages/page1.aspx
返回/pages/page1.aspx
in my case i use this
where
is return http://www.mywebsite.com:80/pages/page1.aspx
is return /pages/page1.aspx
ASP.net 核心:Microsoft.AspNetCore.Http.Extensions
文档微软
我的代码
或
ASP.net core: Microsoft.AspNetCore.Http.Extensions
document of microsoft
my code
or
Uri
类有一个构造函数“根据指定的基本 URI 和相对 URI 字符串初始化 Uri 类的新实例”,因此: microsoft.com/en-us/dotnet/api/system.web.http?view=aspnet-webapi-5.2" rel="nofollow noreferrer">System.Web.Http 命名空间,ApiController.Request 属性
The
Uri
class has a constructor that "Initializes a new instance of the Uri class based on the specified base URI and relative URI string", thus:Given
Request.RequestUri
in System.Web.Http Namespace, ApiController.Request Property好吧,如果您在 Asp.Net 中执行此操作或有权访问 HttpContext.Current.Request
我想说这些是更简单、更通用的获取它们的方法:
我希望这会有所帮助。 :)
Well if you are doing this in Asp.Net or have access to HttpContext.Current.Request
I'd say these are easier and more general ways of getting them:
I hope this helps. :)
以下 (C#) 代码应该可以解决问题
The following (C#) code should do the trick
尽管 @Rick 对此问题有公认的答案,但实际上有一种更短的方法可以做到这一点,即使用名称不佳的
Uri.GetLeftPart()
方法。然而,
GetLeftPart()
有一个问题。 如果该端口是该方案的默认端口,则会将其删除。 由于端口 80 是 http 的默认端口,因此上面示例中的GetLeftPart()
的输出将为http://www.mywebsite.com
。如果端口号不是 80,它将包含在结果中。
Even though @Rick has the accepted answer for this question, there's actually a shorter way to do this, using the poorly named
Uri.GetLeftPart()
method.There is one catch to
GetLeftPart()
, however. If the port is the default port for the scheme, it will strip it out. Since port 80 is the default port for http, the output ofGetLeftPart()
in my example above will behttp://www.mywebsite.com
.If the port number had been something other than 80, it would be included in the result.