Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HelpPageConfig.cs 6.4KB

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