PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.Net.Http.Formatting/System/Net/Http/Formatting/MediaTypeFormatterExtensionMethods.cs

#
C# | 221 lines | 109 code | 17 blank | 95 comment | 16 complexity | c380a092ca2c6d5311671b20c45433ce MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.Net.Http.Formatting
  5. {
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Net.Http.Headers;
  9. /// <summary>
  10. /// Extension methods to provide convenience in adding <see cref="MediaTypeMapping"/>
  11. /// items to a <see cref="MediaTypeFormatter"/>.
  12. /// </summary>
  13. public static class MediaTypeFormatterExtensionMethods
  14. {
  15. /// <summary>
  16. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  17. /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s containing
  18. /// a specific query parameter and value.
  19. /// </summary>
  20. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="QueryStringMapping"/> item.</param>
  21. /// <param name="queryStringParameterName">The name of the query parameter.</param>
  22. /// <param name="queryStringParameterValue">The value assigned to that query parameter.</param>
  23. /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate
  24. /// with a <see cref="Uri"/> containing a query string matching <paramref name="queryStringParameterName"/>
  25. /// and <paramref name="queryStringParameterValue"/>.</param>
  26. public static void AddQueryStringMapping(
  27. this MediaTypeFormatter formatter,
  28. string queryStringParameterName,
  29. string queryStringParameterValue,
  30. MediaTypeHeaderValue mediaType)
  31. {
  32. if (formatter == null)
  33. {
  34. throw new ArgumentNullException("formatter");
  35. }
  36. QueryStringMapping mapping = new QueryStringMapping(queryStringParameterName, queryStringParameterValue, mediaType);
  37. formatter.MediaTypeMappings.Add(mapping);
  38. }
  39. /// <summary>
  40. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  41. /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s containing
  42. /// a specific query parameter and value.
  43. /// </summary>
  44. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="QueryStringMapping"/> item.</param>
  45. /// <param name="queryStringParameterName">The name of the query parameter.</param>
  46. /// <param name="queryStringParameterValue">The value assigned to that query parameter.</param>
  47. /// <param name="mediaType">The media type to associate
  48. /// with a <see cref="Uri"/> containing a query string matching <paramref name="queryStringParameterName"/>
  49. /// and <paramref name="queryStringParameterValue"/>.</param>
  50. public static void AddQueryStringMapping(
  51. this MediaTypeFormatter formatter,
  52. string queryStringParameterName,
  53. string queryStringParameterValue,
  54. string mediaType)
  55. {
  56. if (formatter == null)
  57. {
  58. throw new ArgumentNullException("formatter");
  59. }
  60. QueryStringMapping mapping = new QueryStringMapping(queryStringParameterName, queryStringParameterValue, mediaType);
  61. formatter.MediaTypeMappings.Add(mapping);
  62. }
  63. /// <summary>
  64. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  65. /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s ending with
  66. /// the given <paramref name="uriPathExtension"/>.
  67. /// </summary>
  68. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="UriPathExtensionMapping"/> item.</param>
  69. /// <param name="uriPathExtension">The string of the <see cref="Uri"/> path extension.</param>
  70. /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate with <see cref="Uri"/>s
  71. /// ending with <paramref name="uriPathExtension"/>.</param>
  72. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")]
  73. public static void AddUriPathExtensionMapping(
  74. this MediaTypeFormatter formatter,
  75. string uriPathExtension,
  76. MediaTypeHeaderValue mediaType)
  77. {
  78. if (formatter == null)
  79. {
  80. throw new ArgumentNullException("formatter");
  81. }
  82. UriPathExtensionMapping mapping = new UriPathExtensionMapping(uriPathExtension, mediaType);
  83. formatter.MediaTypeMappings.Add(mapping);
  84. }
  85. /// <summary>
  86. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  87. /// so that it associates the <paramref name="mediaType"/> with <see cref="Uri"/>s ending with
  88. /// the given <paramref name="uriPathExtension"/>.
  89. /// </summary>
  90. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="UriPathExtensionMapping"/> item.</param>
  91. /// <param name="uriPathExtension">The string of the <see cref="Uri"/> path extension.</param>
  92. /// <param name="mediaType">The string media type to associate with <see cref="Uri"/>s
  93. /// ending with <paramref name="uriPathExtension"/>.</param>
  94. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "There is no meaningful System.Uri representation for a path suffix such as '.xml'")]
  95. public static void AddUriPathExtensionMapping(this MediaTypeFormatter formatter, string uriPathExtension, string mediaType)
  96. {
  97. if (formatter == null)
  98. {
  99. throw new ArgumentNullException("formatter");
  100. }
  101. UriPathExtensionMapping mapping = new UriPathExtensionMapping(uriPathExtension, mediaType);
  102. formatter.MediaTypeMappings.Add(mapping);
  103. }
  104. /// <summary>
  105. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  106. /// so that it associates the <paramref name="mediaType"/> with requests or responses containing
  107. /// <paramref name="mediaRange"/> in the content headers.
  108. /// </summary>
  109. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
  110. /// <param name="mediaRange">The media range that will appear in the content headers.</param>
  111. /// <param name="mediaType">The media type to associate with that <paramref name="mediaRange"/>.</param>
  112. public static void AddMediaRangeMapping(this MediaTypeFormatter formatter, string mediaRange, string mediaType)
  113. {
  114. if (formatter == null)
  115. {
  116. throw new ArgumentNullException("formatter");
  117. }
  118. MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
  119. formatter.MediaTypeMappings.Add(mapping);
  120. }
  121. /// <summary>
  122. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  123. /// so that it associates the <paramref name="mediaType"/> with requests or responses containing
  124. /// <paramref name="mediaRange"/> in the content headers.
  125. /// </summary>
  126. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
  127. /// <param name="mediaRange">The media range that will appear in the content headers.</param>
  128. /// <param name="mediaType">The media type to associate with that <paramref name="mediaRange"/>.</param>
  129. public static void AddMediaRangeMapping(
  130. this MediaTypeFormatter formatter,
  131. MediaTypeHeaderValue mediaRange,
  132. MediaTypeHeaderValue mediaType)
  133. {
  134. if (formatter == null)
  135. {
  136. throw new ArgumentNullException("formatter");
  137. }
  138. MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
  139. formatter.MediaTypeMappings.Add(mapping);
  140. }
  141. /// <summary>
  142. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  143. /// so that it associates the <paramref name="mediaType"/> with a specific HTTP request header field
  144. /// with a specific value.
  145. /// </summary>
  146. /// <remarks><see cref="RequestHeaderMapping"/> checks header fields associated with <see cref="M:HttpRequestMessage.Headers"/> for a match. It does
  147. /// not check header fields associated with <see cref="M:HttpResponseMessage.Headers"/> or <see cref="M:HttpContent.Headers"/> instances.</remarks>
  148. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
  149. /// <param name="headerName">Name of the header to match.</param>
  150. /// <param name="headerValue">The header value to match.</param>
  151. /// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param>
  152. /// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is
  153. /// considered a match if it matches a substring of the actual header value.</param>
  154. /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate
  155. /// with a <see cref="M:HttpRequestMessage.Header"/> entry with a name matching <paramref name="headerName"/>
  156. /// and a value matching <paramref name="headerValue"/>.</param>
  157. public static void AddRequestHeaderMapping(
  158. this MediaTypeFormatter formatter,
  159. string headerName,
  160. string headerValue,
  161. StringComparison valueComparison,
  162. bool isValueSubstring,
  163. MediaTypeHeaderValue mediaType)
  164. {
  165. if (formatter == null)
  166. {
  167. throw new ArgumentNullException("formatter");
  168. }
  169. RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, valueComparison, isValueSubstring, mediaType);
  170. formatter.MediaTypeMappings.Add(mapping);
  171. }
  172. /// <summary>
  173. /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements
  174. /// so that it associates the <paramref name="mediaType"/> with a specific HTTP request header field
  175. /// with a specific value.
  176. /// </summary>
  177. /// <remarks><see cref="RequestHeaderMapping"/> checks header fields associated with <see cref="M:HttpRequestMessage.Headers"/> for a match. It does
  178. /// not check header fields associated with <see cref="M:HttpResponseMessage.Headers"/> or <see cref="M:HttpContent.Headers"/> instances.</remarks>
  179. /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaRangeMapping"/> item.</param>
  180. /// <param name="headerName">Name of the header to match.</param>
  181. /// <param name="headerValue">The header value to match.</param>
  182. /// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param>
  183. /// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is
  184. /// considered a match if it matches a substring of the actual header value.</param>
  185. /// <param name="mediaType">The media type to associate
  186. /// with a <see cref="M:HttpRequestMessage.Header"/> entry with a name matching <paramref name="headerName"/>
  187. /// and a value matching <paramref name="headerValue"/>.</param>
  188. public static void AddRequestHeaderMapping(
  189. this MediaTypeFormatter formatter,
  190. string headerName,
  191. string headerValue,
  192. StringComparison valueComparison,
  193. bool isValueSubstring,
  194. string mediaType)
  195. {
  196. if (formatter == null)
  197. {
  198. throw new ArgumentNullException("formatter");
  199. }
  200. RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, valueComparison, isValueSubstring, mediaType);
  201. formatter.MediaTypeMappings.Add(mapping);
  202. }
  203. }
  204. }