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 RandomDataWebApi . 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 = "RandomDataWebApi.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 ( "~/bin/RandomDataWebApi.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 ( int ) , 8 } ,
45+ // {typeof(string), "sample string"},
46+ // {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
47+ } ) ;
48+
49+ // Extend the following to provide factories for types not handled automatically (those lacking parameterless
50+ // constructors) or for which you prefer to use non-default property values. Line below provides a fallback
51+ // since automatic handling will fail and GeneratePageResult handles only a single type.
52+ #if Handle_PageResultOfT
53+ config . GetHelpPageSampleGenerator ( ) . SampleObjectFactories . Add ( GeneratePageResult ) ;
54+ #endif
55+
56+ // Extend the following to use a preset object directly as the sample for all actions that support a media
57+ // type, regardless of the body parameter or return type. The lines below avoid display of binary content.
58+ // The BsonMediaTypeFormatter (if available) is not used to serialize the TextSample object.
59+ config . SetSampleForMediaType (
60+ new TextSample ( "Binary JSON content. See http://bsonspec.org for details." ) ,
61+ new MediaTypeHeaderValue ( "application/bson" ) ) ;
62+
63+ //// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
64+ //// and have IEnumerable<string> as the body parameter or return type.
65+ //config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));
66+
67+ //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
68+ //// and action named "Put".
69+ //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");
70+
71+ //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
72+ //// on the controller named "Values" and action named "Get" with parameter "id".
73+ //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");
74+
75+ //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
76+ //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
77+ //config.SetActualRequestType(typeof(string), "Values", "Get");
78+
79+ //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
80+ //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
81+ //config.SetActualResponseType(typeof(string), "Values", "Post");
82+ }
83+
84+ #if Handle_PageResultOfT
85+ private static object GeneratePageResult ( HelpPageSampleGenerator sampleGenerator , Type type )
86+ {
87+ if ( type . IsGenericType )
88+ {
89+ Type openGenericType = type . GetGenericTypeDefinition ( ) ;
90+ if ( openGenericType == typeof ( PageResult < > ) )
91+ {
92+ // Get the T in PageResult<T>
93+ Type [ ] typeParameters = type . GetGenericArguments ( ) ;
94+ Debug . Assert ( typeParameters . Length == 1 ) ;
95+
96+ // Create an enumeration to pass as the first parameter to the PageResult<T> constuctor
97+ Type itemsType = typeof ( List < > ) . MakeGenericType ( typeParameters ) ;
98+ object items = sampleGenerator . GetSampleObject ( itemsType ) ;
99+
100+ // Fill in the other information needed to invoke the PageResult<T> constuctor
101+ Type [ ] parameterTypes = new Type [ ] { itemsType , typeof ( Uri ) , typeof ( long ? ) , } ;
102+ object [ ] parameters = new object [ ] { items , null , ( long ) ObjectGenerator . DefaultCollectionSize , } ;
103+
104+ // Call PageResult(IEnumerable<T> items, Uri nextPageLink, long? count) constructor
105+ ConstructorInfo constructor = type . GetConstructor ( parameterTypes ) ;
106+ return constructor . Invoke ( parameters ) ;
107+ }
108+ }
109+
110+ return null ;
111+ }
112+ #endif
113+ }
114+ }
0 commit comments