This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
RouteValuesAddressMetadata.cs
87 lines (76 loc) · 3.23 KB
/
RouteValuesAddressMetadata.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
namespace Microsoft.AspNetCore.Routing
{
/// <summary>
/// Metadata used during link generation to find the associated endpoint using route values.
/// </summary>
[DebuggerDisplay("{DebuggerToString(),nq}")]
public sealed class RouteValuesAddressMetadata : IRouteValuesAddressMetadata
{
private static readonly IReadOnlyDictionary<string, object> EmptyRouteValues =
new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
/// <summary>
/// Creates a new instance of <see cref="RouteValuesAddressMetadata"/> with the provided route name.
/// </summary>
/// <param name="routeName">The route name. Can be null.</param>
public RouteValuesAddressMetadata(string routeName) : this(routeName, EmptyRouteValues)
{
}
/// <summary>
/// Creates a new instance of <see cref="RouteValuesAddressMetadata"/> with the provided required route values.
/// </summary>
/// <param name="requiredValues">The required route values.</param>
public RouteValuesAddressMetadata(IReadOnlyDictionary<string, object> requiredValues) : this(null, requiredValues)
{
}
/// <summary>
/// Creates a new instance of <see cref="RouteValuesAddressMetadata"/> with the provided route name and required route values.
/// </summary>
/// <param name="routeName">The route name. Can be null.</param>
/// <param name="requiredValues">The required route values.</param>
public RouteValuesAddressMetadata(string routeName, IReadOnlyDictionary<string, object> requiredValues)
{
if (requiredValues == null)
{
throw new ArgumentNullException(nameof(requiredValues));
}
RouteName = routeName;
RequiredValues = requiredValues;
}
/// <summary>
/// Gets the route name. Can be null.
/// </summary>
public string RouteName { get; }
/// <summary>
/// Gets the required route values.
/// </summary>
public IReadOnlyDictionary<string, object> RequiredValues { get; }
internal string DebuggerToString()
{
return $"Name: {RouteName} - Required values: {string.Join(", ", FormatValues(RequiredValues))}";
IEnumerable<string> FormatValues(IEnumerable<KeyValuePair<string, object>> values)
{
if (values == null)
{
return Array.Empty<string>();
}
return values.Select(
kvp =>
{
var value = "null";
if (kvp.Value != null)
{
value = "\"" + kvp.Value.ToString() + "\"";
}
return kvp.Key + " = " + value;
});
}
}
}
}