.NET - 获取协议、主机和端口

发布于 2024-07-04 10:09:40 字数 360 浏览 5 评论 0原文

.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 技术交流群。

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

发布评论

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

评论(11

私藏温柔 2024-07-11 10:09:41

与霍尔格的回答非常相似。 如果您需要获取 URL 可以执行以下操作:

Uri uri = Context.Request.Url;         
var scheme = uri.Scheme // returns http, https
var scheme2 = uri.Scheme + Uri.SchemeDelimiter; // returns http://, https://
var host = uri.Host; // return www.mywebsite.com
var port = uri.Port; // returns port number

Uri 类 提供了一整套方法,其中许多方法我没有列出。

在我的实例中,我需要抓取 LocalHost 以及 Port Number,所以这就是我所做的:

var Uri uri = Context.Request.Url;
var host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; 

成功抓取:http://localhost:12345

Very similar to Holger's answer. If you need to grab the URL can do something like:

Uri uri = Context.Request.Url;         
var scheme = uri.Scheme // returns http, https
var scheme2 = uri.Scheme + Uri.SchemeDelimiter; // returns http://, https://
var host = uri.Host; // return www.mywebsite.com
var port = uri.Port; // returns port number

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 the Port Number, so this is what I did:

var Uri uri = Context.Request.Url;
var host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; 

Which successfully grabbed: http://localhost:12345

画骨成沙 2024-07-11 10:09:41

Request.Url 将返回请求的 Uri。 一旦你拥有了它,你就可以检索几乎任何你想要的东西。 要获取协议,请调用 Scheme 属性。

示例:

Uri url = Request.Url;
string protocol = url.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:

Uri url = Request.Url;
string protocol = url.Scheme;

Hope this helps.

大海や 2024-07-11 10:09:41

甚至更短的方式,可能需要更新的 ASP.Net:UriComponents

string authority = Request.Url.GetComponents(UriComponents.SchemeAndServer,UriFormat.Unescaped)

枚举允许您指定要包含的 URI 的哪个或哪些组件。

Even shorter way, may require newer ASP.Net:

string authority = Request.Url.GetComponents(UriComponents.SchemeAndServer,UriFormat.Unescaped)

The UriComponents enum lets you specify which component(s) of the URI you want to include.

咿呀咿呀哟 2024-07-11 10:09:41

一种更结构化的方法是使用 UriBuilder。 这避免了直接的字符串操作。

var builder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port);

A more structured way to get this is to use UriBuilder. This avoids direct string manipulation.

var builder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port);
稀香 2024-07-11 10:09:41

就我而言,

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Authority;

可以获取

https://www.mywebsite.com:80

In my case

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Authority;

works to get

https://www.mywebsite.com:80

浊酒尽余欢 2024-07-11 10:09:41

就我而言,我使用这里

Request.Url.ToString().Remove(Request.Url.ToString().Count() - (Request.ServerVariables["URL"].ToString().Count()))

return

Request.Url.ToString() 

http://www.mywebsite.com:80 /pages/page1.aspx

Request.ServerVariables["URL"].ToString()

返回/pages/page1.aspx

in my case i use this

Request.Url.ToString().Remove(Request.Url.ToString().Count() - (Request.ServerVariables["URL"].ToString().Count()))

where

Request.Url.ToString() 

is return http://www.mywebsite.com:80/pages/page1.aspx

Request.ServerVariables["URL"].ToString()

is return /pages/page1.aspx

厌倦 2024-07-11 10:09:41

ASP.net 核心:Microsoft.AspNetCore.Http.Extensions
文档微软

using Microsoft.AspNetCore.Http.Extensions;

我的代码

var leftPath = new Uri(HttpContext.Request.GetDisplayUrl()).GetLeftPart(UriPartial.Authority);

var leftPath = new Uri("http://website.com:80/pages/page1").GetLeftPart(UriPartial.Authority);

ASP.net core: Microsoft.AspNetCore.Http.Extensions
document of microsoft

using Microsoft.AspNetCore.Http.Extensions;

my code

var leftPath = new Uri(HttpContext.Request.GetDisplayUrl()).GetLeftPart(UriPartial.Authority);

or

var leftPath = new Uri("http://website.com:80/pages/page1").GetLeftPart(UriPartial.Authority);
如梦初醒的夏天 2024-07-11 10:09:41

Uri有一个构造函数“根据指定的基本 URI 和相对 URI 字符串初始化 Uri 类的新实例”,因此

var hostPortOnlyUrl = new Uri(Request.RequestUri, "/");

: 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:

var hostPortOnlyUrl = new Uri(Request.RequestUri, "/");

Given Request.RequestUri in System.Web.Http Namespace, ApiController.Request Property

迎风吟唱 2024-07-11 10:09:41

好吧,如果您在 Asp.Net 中执行此操作或有权访问 HttpContext.Current.Request
我想说这些是更简单、更通用的获取它们的方法:

var scheme = Request.Url.Scheme; // will get http, https, etc.
var host = Request.Url.Host; // will get www.mywebsite.com
var port = Request.Url.Port; // will get the port
var path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx

我希望这会有所帮助。 :)

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:

var scheme = Request.Url.Scheme; // will get http, https, etc.
var host = Request.Url.Host; // will get www.mywebsite.com
var port = Request.Url.Port; // will get the port
var path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx

I hope this helps. :)

葬シ愛 2024-07-11 10:09:41

以下 (C#) 代码应该可以解决问题

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;

The following (C#) code should do the trick

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
感性 2024-07-11 10:09:40

尽管 @Rick 对此问题有公认的答案,但实际上有一种更短的方法可以做到这一点,即使用名称不佳的 Uri.GetLeftPart() 方法。

Uri url = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string output = url.GetLeftPart(UriPartial.Authority);

然而,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.

Uri url = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string output = url.GetLeftPart(UriPartial.Authority);

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 of GetLeftPart() in my example above will be http://www.mywebsite.com.

If the port number had been something other than 80, it would be included in the result.

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