forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormattingUtilities.cs
More file actions
148 lines (120 loc) · 5.35 KB
/
Copy pathFormattingUtilities.cs
File metadata and controls
148 lines (120 loc) · 5.35 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ModelBinding.Binders;
using System.Web.Http.Properties;
using System.Web.Http.Routing;
using System.Web.Http.ValueProviders;
using System.Web.Http.ValueProviders.Providers;
namespace System.Web.Http.Tracing
{
/// <summary>
/// General purpose utilities to format strings used in tracing.
/// </summary>
internal static class FormattingUtilities
{
public static readonly string NullMessage = "null";
public static string ActionArgumentsToString(IDictionary<string, object> actionArguments)
{
Contract.Assert(actionArguments != null);
return String.Join(", ",
actionArguments.Keys.Select<string, string>(
(k) => k + "=" + ValueToString(actionArguments[k], CultureInfo.CurrentCulture)));
}
public static string ActionDescriptorToString(HttpActionDescriptor actionDescriptor)
{
Contract.Assert(actionDescriptor != null);
string parameterList = String.Join(", ",
actionDescriptor.GetParameters().Select<HttpParameterDescriptor, string>(
(p) => p.ParameterType.Name + " " + p.ParameterName));
return actionDescriptor.ActionName + "(" + parameterList + ")";
}
public static string ActionInvokeToString(HttpActionContext actionContext)
{
Contract.Assert(actionContext != null);
return ActionInvokeToString(actionContext.ActionDescriptor.ActionName, actionContext.ActionArguments);
}
public static string ActionInvokeToString(string actionName, IDictionary<string, object> arguments)
{
Contract.Assert(actionName != null);
Contract.Assert(arguments != null);
return actionName + "(" + ActionArgumentsToString(arguments) + ")";
}
public static string FormattersToString(IEnumerable<MediaTypeFormatter> formatters)
{
Contract.Assert(formatters != null);
return String.Join(", ", formatters.Select<MediaTypeFormatter, string>((f) => f.GetType().Name));
}
public static string ModelBinderToString(ModelBinderProvider provider)
{
Contract.Assert(provider != null);
CompositeModelBinderProvider composite = provider as CompositeModelBinderProvider;
if (composite == null)
{
return provider.GetType().Name;
}
string modelBinderList = String.Join(", ", composite.Providers.Select<ModelBinderProvider, string>(ModelBinderToString));
return provider.GetType().Name + "(" + modelBinderList + ")";
}
public static string ModelStateToString(ModelStateDictionary modelState)
{
Contract.Assert(modelState != null);
if (modelState.IsValid)
{
return String.Empty;
}
StringBuilder modelStateBuilder = new StringBuilder();
foreach (string key in modelState.Keys)
{
ModelState state = modelState[key];
if (state.Errors.Count > 0)
{
foreach (ModelError error in state.Errors)
{
string errorString = Error.Format(SRResources.TraceModelStateErrorMessage,
key,
error.ErrorMessage);
if (modelStateBuilder.Length > 0)
{
modelStateBuilder.Append(',');
}
modelStateBuilder.Append(errorString);
}
}
}
return modelStateBuilder.ToString();
}
public static string RouteToString(IHttpRouteData routeData)
{
Contract.Assert(routeData != null);
return String.Join(",", routeData.Values.Select((pair) => Error.Format("{0}:{1}", pair.Key, pair.Value)));
}
public static string ValueProviderToString(IValueProvider provider)
{
Contract.Assert(provider != null);
CompositeValueProvider composite = provider as CompositeValueProvider;
if (composite == null)
{
return provider.GetType().Name;
}
string providerList = String.Join(", ", composite.Select<IValueProvider, string>(ValueProviderToString));
return provider.GetType().Name + "(" + providerList + ")";
}
public static string ValueToString(object value, CultureInfo cultureInfo)
{
Contract.Assert(cultureInfo != null);
if (value == null)
{
return NullMessage;
}
return Convert.ToString(value, cultureInfo);
}
}
}