本文整理汇总了C#中MediaTypeFormatterCollection.FirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeFormatterCollection.FirstOrDefault方法的具体用法?C# MediaTypeFormatterCollection.FirstOrDefault怎么用?C# MediaTypeFormatterCollection.FirstOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MediaTypeFormatterCollection
的用法示例。
在下文中一共展示了MediaTypeFormatterCollection.FirstOrDefault方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFormatter
/***
** HttpClient
***/
/// <summary>Get the formatter for an HTTP content type.</summary>
/// <param name="formatters">The formatters used for serializing and deserializing message bodies.</param>
/// <param name="contentType">The HTTP content type (or <c>null</c> to automatically select one).</param>
/// <exception cref="InvalidOperationException">No MediaTypeFormatters are available on the API client for this content type.</exception>
public virtual MediaTypeFormatter GetFormatter(MediaTypeFormatterCollection formatters, MediaTypeHeaderValue contentType = null)
{
if (!formatters.Any())
throw new InvalidOperationException("No MediaTypeFormatters are available on the fluent client.");
MediaTypeFormatter formatter = contentType != null
? formatters.FirstOrDefault(f => f.MediaTypeMappings.Any(m => m.MediaType.MediaType == contentType.MediaType))
: formatters.FirstOrDefault();
if (formatter == null)
throw new InvalidOperationException(String.Format("No MediaTypeFormatters are available on the fluent client for the '{0}' content-type.", contentType));
return formatter;
}
示例2: ConfigureFormatters
private static void ConfigureFormatters(MediaTypeFormatterCollection formatters)
{
// Remove unnecessary formatters
MediaTypeFormatter jqueryFormatter = formatters.FirstOrDefault(x => x.GetType() == typeof(JQueryMvcFormUrlEncodedFormatter));
formatters.Remove(formatters.XmlFormatter);
formatters.Remove(formatters.FormUrlEncodedFormatter);
formatters.Remove(jqueryFormatter);
// Suppressing the IRequiredMemberSelector for all formatters
foreach (var formatter in formatters)
{
formatter.RequiredMemberSelector = new SuppressedRequiredMemberSelector();
}
}