-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
HostAttribute.cs
60 lines (52 loc) · 1.98 KB
/
HostAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Shared;
namespace Microsoft.AspNetCore.Routing;
/// <summary>
/// Attribute for providing host metdata that is used during routing.
/// </summary>
[DebuggerDisplay("{ToString(),nq}")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class HostAttribute : Attribute, IHostMetadata
{
/// <summary>
/// Initializes a new instance of the <see cref="HostAttribute" /> class.
/// </summary>
/// <param name="host">
/// The host used during routing.
/// Host should be Unicode rather than punycode, and may have a port.
/// </param>
public HostAttribute(string host) : this(new[] { host })
{
ArgumentNullException.ThrowIfNull(host);
}
/// <summary>
/// Initializes a new instance of the <see cref="HostAttribute" /> class.
/// </summary>
/// <param name="hosts">
/// The hosts used during routing.
/// Hosts should be Unicode rather than punycode, and may have a port.
/// An empty collection means any host will be accepted.
/// </param>
public HostAttribute(params string[] hosts)
{
ArgumentNullException.ThrowIfNull(hosts);
Hosts = hosts.ToArray();
}
/// <summary>
/// Returns a read-only collection of hosts used during routing.
/// Hosts will be Unicode rather than punycode, and may have a port.
/// An empty collection means any host will be accepted.
/// </summary>
public IReadOnlyList<string> Hosts { get; }
/// <inheritdoc/>
public override string ToString()
{
var hostsDisplay = (Hosts.Count == 0)
? "*:*"
: string.Join(",", Hosts.Select(h => h.Contains(':') ? h : h + ":*"));
return DebuggerHelpers.GetDebugText(nameof(Hosts), hostsDisplay);
}
}