forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelBinderConfig.cs
More file actions
101 lines (89 loc) · 4.73 KB
/
ModelBinderConfig.cs
File metadata and controls
101 lines (89 loc) · 4.73 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
// 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.Diagnostics.CodeAnalysis;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Properties;
namespace System.Web.Http.ModelBinding
{
// REVIEW: Need a way to get the user's resource string choice
// Provides configuration settings common to the new model binding system.
public static class ModelBinderConfig
{
private static string _resourceClassKey;
private static ModelBinderErrorMessageProvider _typeConversionErrorMessageProvider;
private static ModelBinderErrorMessageProvider _valueRequiredErrorMessageProvider;
public static string ResourceClassKey
{
get { return _resourceClassKey ?? String.Empty; }
set { _resourceClassKey = value; }
}
public static ModelBinderErrorMessageProvider TypeConversionErrorMessageProvider
{
get
{
if (_typeConversionErrorMessageProvider == null)
{
_typeConversionErrorMessageProvider = DefaultTypeConversionErrorMessageProvider;
}
return _typeConversionErrorMessageProvider;
}
set { _typeConversionErrorMessageProvider = value; }
}
public static ModelBinderErrorMessageProvider ValueRequiredErrorMessageProvider
{
get
{
if (_valueRequiredErrorMessageProvider == null)
{
_valueRequiredErrorMessageProvider = DefaultValueRequiredErrorMessageProvider;
}
return _valueRequiredErrorMessageProvider;
}
set { _valueRequiredErrorMessageProvider = value; }
}
private static string DefaultTypeConversionErrorMessageProvider(HttpActionContext actionContext, ModelMetadata modelMetadata, object incomingValue)
{
return GetResourceCommon(actionContext, modelMetadata, incomingValue, GetValueInvalidResource);
}
private static string DefaultValueRequiredErrorMessageProvider(HttpActionContext actionContext, ModelMetadata modelMetadata, object incomingValue)
{
return GetResourceCommon(actionContext, modelMetadata, incomingValue, GetValueRequiredResource);
}
private static string GetResourceCommon(HttpActionContext actionContext, ModelMetadata modelMetadata, object incomingValue, Func<HttpActionContext, string> resourceAccessor)
{
string displayName = modelMetadata.GetDisplayName();
string errorMessageTemplate = resourceAccessor(actionContext);
return Error.Format(errorMessageTemplate, incomingValue, displayName);
}
private static string GetUserResourceString(HttpActionContext actionContext, string resourceName)
{
return GetUserResourceString(actionContext, resourceName, ResourceClassKey);
}
// If the user specified a ResourceClassKey try to load the resource they specified.
// If the class key is invalid, an exception will be thrown.
// If the class key is valid but the resource is not found, it returns null, in which
// case it will fall back to the MVC default error message.
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "resourceName", Justification = "Temporary")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "resourceClassKey", Justification = "Temporary")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "actionContext", Justification = "Temporary")]
internal static string GetUserResourceString(HttpActionContext actionContext, string resourceName, string resourceClassKey)
{
#if false
return (!String.IsNullOrEmpty(resourceClassKey) && (actionContext != null) && (actionContext.HttpContext != null))
? actionContext.HttpContext.GetGlobalResourceObject(resourceClassKey, resourceName, CultureInfo.CurrentUICulture) as string
: null;
#else
return null;
#endif
}
private static string GetValueInvalidResource(HttpActionContext actionContext)
{
return GetUserResourceString(actionContext, "PropertyValueInvalid") ?? SRResources.ModelBinderConfig_ValueInvalid;
}
private static string GetValueRequiredResource(HttpActionContext actionContext)
{
return GetUserResourceString(actionContext, "PropertyValueRequired") ?? SRResources.ModelBinderConfig_ValueRequired;
}
}
}