本文整理汇总了C#中MediaType.IsSubsetOf方法的典型用法代码示例。如果您正苦于以下问题:C# MediaType.IsSubsetOf方法的具体用法?C# MediaType.IsSubsetOf怎么用?C# MediaType.IsSubsetOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MediaType
的用法示例。
在下文中一共展示了MediaType.IsSubsetOf方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanWriteResult
/// <inheritdoc />
public virtual bool CanWriteResult(OutputFormatterCanWriteContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (SupportedMediaTypes.Count == 0)
{
var message = Resources.FormatFormatter_NoMediaTypes(
GetType().FullName,
nameof(SupportedMediaTypes));
throw new InvalidOperationException(message);
}
if (!CanWriteType(context.ObjectType))
{
return false;
}
if (!context.ContentType.HasValue)
{
// If the desired content type is set to null, then the current formatter can write anything
// it wants.
context.ContentType = new StringSegment(SupportedMediaTypes[0]);
return true;
}
else
{
// Confirm this formatter supports a more specific media type than requested e.g. OK if "text/*"
// requested and formatter supports "text/plain". contentType is typically what we got in an Accept
// header.
var parsedContentType = new MediaType(context.ContentType);
for (var i = 0; i < SupportedMediaTypes.Count; i++)
{
var supportedMediaType = new MediaType(SupportedMediaTypes[i]);
if (supportedMediaType.IsSubsetOf(parsedContentType))
{
context.ContentType = new StringSegment(SupportedMediaTypes[i]);
return true;
}
}
}
return false;
}
示例2: GetSupportedContentTypes
/// <inheritdoc />
public virtual IReadOnlyList<string> GetSupportedContentTypes(
string contentType,
Type objectType)
{
if (SupportedMediaTypes.Count == 0)
{
var message = Resources.FormatFormatter_NoMediaTypes(
GetType().FullName,
nameof(SupportedMediaTypes));
throw new InvalidOperationException(message);
}
if (!CanWriteType(objectType))
{
return null;
}
if (contentType == null)
{
// If contentType is null, then any type we support is valid.
return SupportedMediaTypes;
}
else
{
List<string> mediaTypes = null;
var parsedContentType = new MediaType(contentType);
// Confirm this formatter supports a more specific media type than requested e.g. OK if "text/*"
// requested and formatter supports "text/plain". Treat contentType like it came from an Accept header.
foreach (var mediaType in SupportedMediaTypes)
{
var parsedMediaType = new MediaType(mediaType);
if (parsedMediaType.IsSubsetOf(parsedContentType))
{
if (mediaTypes == null)
{
mediaTypes = new List<string>();
}
mediaTypes.Add(mediaType);
}
}
return mediaTypes;
}
}
示例3: IsSubsetOf
public void IsSubsetOf(string set, string subset, bool expectedResult)
{
// Arrange
var setMediaType = new MediaType(set);
var subSetMediaType = new MediaType(subset);
// Act
var result = subSetMediaType.IsSubsetOf(setMediaType);
// Assert
Assert.Equal(expectedResult, result);
}
示例4: IsSuperSetOfAnySupportedMediaType
private bool IsSuperSetOfAnySupportedMediaType(string contentType, MediaTypeCollection supportedMediaTypes)
{
var parsedContentType = new MediaType(contentType);
for (var i = 0; i < supportedMediaTypes.Count; i++)
{
var supportedMediaType = new MediaType(supportedMediaTypes[i]);
if (supportedMediaType.IsSubsetOf(parsedContentType))
{
return true;
}
}
return false;
}
示例5: IsSubsetOfAnyContentType
private bool IsSubsetOfAnyContentType(string requestMediaType)
{
var parsedRequestMediaType = new MediaType(requestMediaType);
for (var i = 0; i < ContentTypes.Count; i++)
{
var contentTypeMediaType = new MediaType(ContentTypes[i]);
if (parsedRequestMediaType.IsSubsetOf(contentTypeMediaType))
{
return true;
}
}
return false;
}
示例6: SelectFormatterUsingSortedAcceptHeadersAndContentTypes
/// <summary>
/// Selects the <see cref="IOutputFormatter"/> to write the response based on the content type values
/// present in <paramref name="sortedAcceptableContentTypes"/> and <paramref name="possibleOutputContentTypes"/>.
/// </summary>
/// <param name="formatterContext">The <see cref="OutputFormatterWriteContext"/>.</param>
/// <param name="formatters">
/// The list of <see cref="IOutputFormatter"/> instances to consider.
/// </param>
/// <param name="sortedAcceptableContentTypes">
/// The ordered content types from the <c>Accept</c> header, sorted by descending q-value.
/// </param>
/// <param name="possibleOutputContentTypes">
/// The ordered content types from <see cref="ObjectResult.ContentTypes"/> in descending priority order.
/// </param>
/// <returns>
/// The selected <see cref="IOutputFormatter"/> or <c>null</c> if no formatter can write the response.
/// </returns>
protected virtual IOutputFormatter SelectFormatterUsingSortedAcceptHeadersAndContentTypes(
OutputFormatterWriteContext formatterContext,
IList<IOutputFormatter> formatters,
IList<MediaTypeSegmentWithQuality> sortedAcceptableContentTypes,
MediaTypeCollection possibleOutputContentTypes)
{
for (var i = 0; i < sortedAcceptableContentTypes.Count; i++)
{
var acceptableContentType = new MediaType(sortedAcceptableContentTypes[i].MediaType);
for (var j = 0; j < possibleOutputContentTypes.Count; j++)
{
var candidateContentType = new MediaType(possibleOutputContentTypes[j]);
if (candidateContentType.IsSubsetOf(acceptableContentType))
{
for (var k = 0; k < formatters.Count; k++)
{
var formatter = formatters[k];
formatterContext.ContentType = new StringSegment(possibleOutputContentTypes[j]);
if (formatter.CanWriteResult(formatterContext))
{
return formatter;
}
}
}
}
}
return null;
}
示例7: InAcceptableMediaTypes
private static bool InAcceptableMediaTypes(StringSegment mediaType, MediaTypeCollection acceptableMediaTypes)
{
if (acceptableMediaTypes.Count == 0)
{
return true;
}
var parsedMediaType = new MediaType(mediaType);
for (int i = 0; i < acceptableMediaTypes.Count; i++)
{
var acceptableMediaType = new MediaType(acceptableMediaTypes[i]);
if (acceptableMediaType.IsSubsetOf(parsedMediaType))
{
return true;
}
}
return false;
}