ASP.NET:如何获取没有任何子域的域名?

发布于 2024-10-10 05:19:13 字数 584 浏览 0 评论 0原文

我一直在这里搜索,但我似乎找不到这个问题的答案。我很想知道是否有一种方法可以只提供 HttpContext.Current.Request.Url 中的主域?

示例:

http://www.example.com > example.com
http://test.example.com > example.com
http://example.com > example.com

提前致谢。

编辑

只是为了澄清一点。这仅用于我自己的域,不会用于现有的每个域。
目前我需要处理三个后缀。

  • .com
  • .ca
  • .local

I've been searching here on SO but I can't seem to find the answer to this question. I'm having a heck of a time figuring out if there's a method that will give me just the main domain from the HttpContext.Current.Request.Url?

Examples:

http://www.example.com > example.com
http://test.example.com > example.com
http://example.com > example.com

Thanks in advance.

Edit

just to clarify a bit. This is for use on my own domains only and not going to be used on every domain in existence.
There's currently three suffixes that I need to be able to deal with.

  • .com
  • .ca
  • .local

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

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

发布评论

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

评论(4

薆情海 2024-10-17 05:19:13
public static void Main() {
    var uri = new Uri("http://test.example.com");

    var fullDomain = uri.GetComponents(UriComponents.Host, UriFormat.SafeUnescaped);
    var domainParts = fullDomain
        .Split('.') // ["test", "example", "com"]
        .Reverse()  // ["com", "example", "test"]
        .Take(2)    // ["com", "example"]
        .Reverse(); // ["example", "com"]
    var domain = String.Join(".", domainParts);
}
public static void Main() {
    var uri = new Uri("http://test.example.com");

    var fullDomain = uri.GetComponents(UriComponents.Host, UriFormat.SafeUnescaped);
    var domainParts = fullDomain
        .Split('.') // ["test", "example", "com"]
        .Reverse()  // ["com", "example", "test"]
        .Take(2)    // ["com", "example"]
        .Reverse(); // ["example", "com"]
    var domain = String.Join(".", domainParts);
}
裸钻 2024-10-17 05:19:13

有关允许任意注册的后缀列表,请参阅此处

找到该列表中完整域名的最长后缀,然后返回该后缀之前的最后一个 . 之后的所有内容。

See here for a list of suffixes that allow arbitrary registrations.

Find the longest suffix of the full domain name in that list, then return everything after the last . before that suffix.

勿挽旧人 2024-10-17 05:19:13

获取顶级域的列表,并将每个域与该列表进行匹配,匹配后仅取 1 个单词。

(您可能需要添加一些对 .co 的支持。
等等...

Get a list of top level domains, and match every domain with that list, taking only 1 word after the match.

(you might need to add some support for .co.
ect...

孤独岁月 2024-10-17 05:19:13

这是我提出的想法。
@SLaks,我很想听听您对此的想法。

    ''# First we fix the StackOverflow code coloring issue.
    <Extension()>
    Public Function PrimaryDomain(ByVal url As Uri) As String

        If url.Host.Contains("example.com") Then Return "example.com"
        If url.Host.Contains("example.ca") Then Return "example.ca"
        If url.Host.Contains("example.local") Then Return "example.local"
        If url.Host.Contains("localhost") Then Return "localhost"

        Throw New Exception("The url host was not recognized as a known host name for this domain.")
    End Function

Here's the idea that I've come up with.
@SLaks, I'd love to here your thoughts on this.

    ''# First we fix the StackOverflow code coloring issue.
    <Extension()>
    Public Function PrimaryDomain(ByVal url As Uri) As String

        If url.Host.Contains("example.com") Then Return "example.com"
        If url.Host.Contains("example.ca") Then Return "example.ca"
        If url.Host.Contains("example.local") Then Return "example.local"
        If url.Host.Contains("localhost") Then Return "localhost"

        Throw New Exception("The url host was not recognized as a known host name for this domain.")
    End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文