1
+ // Uncomment the following to provide samples for PageResult<T>. Must also add the Microsoft.AspNet.WebApi.OData
2
+ // package to your project.
3
+ ////#define Handle_PageResultOfT
4
+
5
+ using System ;
6
+ using System . Collections ;
7
+ using System . Collections . Generic ;
8
+ using System . Diagnostics ;
9
+ using System . Diagnostics . CodeAnalysis ;
10
+ using System . Linq ;
11
+ using System . Net . Http . Headers ;
12
+ using System . Reflection ;
13
+ using System . Web ;
14
+ using System . Web . Http ;
15
+ #if Handle_PageResultOfT
16
+ using System . Web . Http . OData ;
17
+ #endif
18
+
19
+ namespace RestfulApi . Areas . HelpPage
20
+ {
21
+ /// <summary>
22
+ /// Use this class to customize the Help Page.
23
+ /// For example you can set a custom <see cref="System.Web.Http.Description.IDocumentationProvider"/> to supply the documentation
24
+ /// or you can provide the samples for the requests/responses.
25
+ /// </summary>
26
+ public static class HelpPageConfig
27
+ {
28
+ [ SuppressMessage ( "Microsoft.Globalization" , "CA1303:Do not pass literals as localized parameters" ,
29
+ MessageId = "RestfulApi.Areas.HelpPage.TextSample.#ctor(System.String)" ,
30
+ Justification = "End users may choose to merge this string with existing localized resources." ) ]
31
+ [ SuppressMessage ( "Microsoft.Naming" , "CA2204:Literals should be spelled correctly" ,
32
+ MessageId = "bsonspec" ,
33
+ Justification = "Part of a URI." ) ]
34
+ public static void Register ( HttpConfiguration config )
35
+ {
36
+ //// Uncomment the following to use the documentation from XML documentation file.
37
+ //config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
38
+
39
+ //// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
40
+ //// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type
41
+ //// formats by the available formatters.
42
+ //config.SetSampleObjects(new Dictionary<Type, object>
43
+ //{
44
+ // {typeof(string), "sample string"},
45
+ // {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
46
+ //});
47
+
48
+ // Extend the following to provide factories for types not handled automatically (those lacking parameterless
49
+ // constructors) or for which you prefer to use non-default property values. Line below provides a fallback
50
+ // since automatic handling will fail and GeneratePageResult handles only a single type.
51
+ #if Handle_PageResultOfT
52
+ config . GetHelpPageSampleGenerator ( ) . SampleObjectFactories . Add ( GeneratePageResult ) ;
53
+ #endif
54
+
55
+ // Extend the following to use a preset object directly as the sample for all actions that support a media
56
+ // type, regardless of the body parameter or return type. The lines below avoid display of binary content.
57
+ // The BsonMediaTypeFormatter (if available) is not used to serialize the TextSample object.
58
+ config . SetSampleForMediaType (
59
+ new TextSample ( "Binary JSON content. See http://bsonspec.org for details." ) ,
60
+ new MediaTypeHeaderValue ( "application/bson" ) ) ;
61
+
62
+ //// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
63
+ //// and have IEnumerable<string> as the body parameter or return type.
64
+ //config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));
65
+
66
+ //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
67
+ //// and action named "Put".
68
+ //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");
69
+
70
+ //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
71
+ //// on the controller named "Values" and action named "Get" with parameter "id".
72
+ //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");
73
+
74
+ //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
75
+ //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
76
+ //config.SetActualRequestType(typeof(string), "Values", "Get");
77
+
78
+ //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
79
+ //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
80
+ //config.SetActualResponseType(typeof(string), "Values", "Post");
81
+ }
82
+
83
+ #if Handle_PageResultOfT
84
+ private static object GeneratePageResult ( HelpPageSampleGenerator sampleGenerator , Type type )
85
+ {
86
+ if ( type . IsGenericType )
87
+ {
88
+ Type openGenericType = type . GetGenericTypeDefinition ( ) ;
89
+ if ( openGenericType == typeof ( PageResult < > ) )
90
+ {
91
+ // Get the T in PageResult<T>
92
+ Type [ ] typeParameters = type . GetGenericArguments ( ) ;
93
+ Debug . Assert ( typeParameters . Length == 1 ) ;
94
+
95
+ // Create an enumeration to pass as the first parameter to the PageResult<T> constuctor
96
+ Type itemsType = typeof ( List < > ) . MakeGenericType ( typeParameters ) ;
97
+ object items = sampleGenerator . GetSampleObject ( itemsType ) ;
98
+
99
+ // Fill in the other information needed to invoke the PageResult<T> constuctor
100
+ Type [ ] parameterTypes = new Type [ ] { itemsType , typeof ( Uri ) , typeof ( long ? ) , } ;
101
+ object [ ] parameters = new object [ ] { items , null , ( long ) ObjectGenerator . DefaultCollectionSize , } ;
102
+
103
+ // Call PageResult(IEnumerable<T> items, Uri nextPageLink, long? count) constructor
104
+ ConstructorInfo constructor = type . GetConstructor ( parameterTypes ) ;
105
+ return constructor . Invoke ( parameters ) ;
106
+ }
107
+ }
108
+
109
+ return null ;
110
+ }
111
+ #endif
112
+ }
113
+ }
0 commit comments