本文整理汇总了C#中Microsoft.Net.Http.Headers.MediaTypeHeaderValue类的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeHeaderValue类的具体用法?C# MediaTypeHeaderValue怎么用?C# MediaTypeHeaderValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MediaTypeHeaderValue类属于Microsoft.Net.Http.Headers命名空间,在下文中一共展示了MediaTypeHeaderValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Ctor_AddNameAndQuality_QualityParameterAdded
public void Ctor_AddNameAndQuality_QualityParameterAdded()
{
var mediaType = new MediaTypeHeaderValue("application/xml", 0.08);
Assert.Equal(0.08, mediaType.Quality);
Assert.Equal("application/xml", mediaType.MediaType);
Assert.Equal(1, mediaType.Parameters.Count);
}
示例2: CanWriteResult
public override bool CanWriteResult([NotNull]OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
var type = context.Object.GetType();
var request = context.ActionContext.HttpContext.Request;
if (request != null)
{
IEdmModel model = request.ODataProperties().Model;
if (model != null)
{
ODataPayloadKind? payloadKind = null;
Type elementType;
if (typeof(IEdmObject).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) ||
(type.IsCollection(out elementType) && typeof(IEdmObject).GetTypeInfo().IsAssignableFrom(elementType.GetTypeInfo())))
{
payloadKind = GetEdmObjectPayloadKind(type, request);
}
else
{
payloadKind = GetClrObjectResponsePayloadKind(type, model, request);
}
return payloadKind == null ? false : _payloadKinds.Contains(payloadKind.Value);
}
}
return false;
}
示例3: ExecuteResultAsync
public override async Task ExecuteResultAsync([NotNull] ActionContext context)
{
var response = context.HttpContext.Response;
MediaTypeHeaderValue contentTypeHeader;
if (string.IsNullOrEmpty(ContentType))
{
contentTypeHeader = new MediaTypeHeaderValue("text/plain");
}
else
{
contentTypeHeader = new MediaTypeHeaderValue(ContentType);
}
contentTypeHeader.Encoding = ContentEncoding ?? Encodings.UTF8EncodingWithoutBOM;
response.ContentType = contentTypeHeader.ToString();
if (StatusCode != null)
{
response.StatusCode = StatusCode.Value;
}
if (Content != null)
{
await response.WriteAsync(Content, contentTypeHeader.Encoding);
}
}
示例4: GetBoundary
private static string GetBoundary(MediaTypeHeaderValue contentType) {
var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary);
if(string.IsNullOrWhiteSpace(boundary)) {
throw new InvalidOperationException("Missing content-type boundary.");
}
return boundary;
}
示例5: ExecuteAsync
/// <summary>
/// Asynchronously renders the specified <paramref name="view"/> to the response body.
/// </summary>
/// <param name="view">The <see cref="IView"/> to render.</param>
/// <param name="actionContext">The <see cref="ActionContext"/> for the current executing action.</param>
/// <param name="viewData">The <see cref="ViewDataDictionary"/> for the view being rendered.</param>
/// <param name="tempData">The <see cref="ITempDataDictionary"/> for the view being rendered.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous rendering.</returns>
public static async Task ExecuteAsync([NotNull] IView view,
[NotNull] ActionContext actionContext,
[NotNull] ViewDataDictionary viewData,
[NotNull] ITempDataDictionary tempData,
[NotNull] HtmlHelperOptions htmlHelperOptions,
MediaTypeHeaderValue contentType)
{
var response = actionContext.HttpContext.Response;
contentType = contentType ?? DefaultContentType;
if (contentType.Encoding == null)
{
// Do not modify the user supplied content type, so copy it instead
contentType = contentType.Copy();
contentType.Encoding = Encoding.UTF8;
}
response.ContentType = contentType.ToString();
using (var writer = new HttpResponseStreamWriter(response.Body, contentType.Encoding))
{
var viewContext = new ViewContext(
actionContext,
view,
viewData,
tempData,
writer,
htmlHelperOptions);
await view.RenderAsync(viewContext);
}
}
示例6: ReturnScript
public async static Task ReturnScript(HttpContext context, string scriptKey, string contentType)
{
var dynamicScript = DynamicScriptManager.GetScript(scriptKey);
if (dynamicScript == null)
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
await context.Response.WriteAsync("File not found!");
}
var mediaType = new MediaTypeHeaderValue(contentType);
mediaType.Encoding = System.Text.Encoding.UTF8;
context.Response.ContentType = mediaType.ToString();
var responseHeaders = context.Response.GetTypedHeaders();
var cacheControl = responseHeaders.CacheControl = new CacheControlHeaderValue();
cacheControl.MaxAge = TimeSpan.FromDays(365);
cacheControl.Private = true;
cacheControl.MustRevalidate = false;
var supportsGzip = dynamicScript.CompressedBytes != null &&
context.Request.GetTypedHeaders().AcceptEncoding.ToString()
.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) >= 0;
byte[] contentBytes;
if (supportsGzip)
{
context.Response.Headers["Content-Encoding"] = "gzip";
contentBytes = dynamicScript.CompressedBytes;
}
else
contentBytes = dynamicScript.UncompressedBytes;
await WriteWithIfModifiedSinceControl(context, contentBytes, dynamicScript.Time);
}
示例7: TwilioResponseResult
public TwilioResponseResult(Action<TwilioResponse> buildResponse)
{
var response = new TwilioResponse();
buildResponse(response);
Content = response.Element.ToString();
ContentType = new MediaTypeHeaderValue("text/xml");
}
示例8: GetSupportedContentTypes
/// <inheritdoc />
public virtual IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(
Type declaredType,
Type runtimeType,
MediaTypeHeaderValue contentType)
{
if (!CanWriteType(declaredType, runtimeType))
{
return null;
}
if (contentType == null)
{
// If contentType is null, then any type we support is valid.
return _supportedMediaTypes.Count > 0 ? _supportedMediaTypes : null;
}
else
{
List<MediaTypeHeaderValue> mediaTypes = null;
foreach (var mediaType in _supportedMediaTypes)
{
if (mediaType.IsSubsetOf(contentType))
{
if (mediaTypes == null)
{
mediaTypes = new List<MediaTypeHeaderValue>();
}
mediaTypes.Add(mediaType);
}
}
return mediaTypes;
}
}
示例9: ValidateContentType
private void ValidateContentType(MediaTypeHeaderValue contentType)
{
if (contentType.Type == "*" || contentType.SubType == "*")
{
throw new ArgumentException(string.Format(Resources.FormatterMappings_NotValidMediaType, contentType));
}
}
示例10: Ctor_MediaTypeValidFormat_SuccessfullyCreated
public void Ctor_MediaTypeValidFormat_SuccessfullyCreated()
{
var mediaType = new MediaTypeHeaderValue("text/plain");
Assert.Equal("text/plain", mediaType.MediaType);
Assert.Equal(0, mediaType.Parameters.Count);
Assert.Null(mediaType.Charset);
}
示例11: ErrorContentResult
/**
* TODO:
* Refactor ErrorContentResult to ErrorContentResult(string responseType = "json")
* and allow JSON & XML responses depending on request type from user.
*/
public static ContentResult ErrorContentResult(
object value,
int status,
string responseType = "json")
{
if (value is string)
{
value = new ApiError { Error = value.ToString() };
}
string content;
MediaTypeHeaderValue contentType;
if (responseType == "xml")
{
content = SerializeToJson(value);
contentType = new MediaTypeHeaderValue("application/xml");
}
else
{
content = SerializeToJson(value);
contentType = new MediaTypeHeaderValue("application/json");
}
return new ContentResult { StatusCode = status, Content = content, ContentType = contentType };
}
示例12: MediaType_SetAndGetMediaType_MatchExpectations
public void MediaType_SetAndGetMediaType_MatchExpectations()
{
var mediaType = new MediaTypeHeaderValue("text/plain");
Assert.Equal("text/plain", mediaType.MediaType);
mediaType.MediaType = "application/xml";
Assert.Equal("application/xml", mediaType.MediaType);
}
示例13: Copy_SimpleMediaType_Copied
public void Copy_SimpleMediaType_Copied()
{
var mediaType0 = new MediaTypeHeaderValue("text/plain");
var mediaType1 = mediaType0.Copy();
Assert.NotSame(mediaType0, mediaType1);
Assert.Same(mediaType0.MediaType, mediaType1.MediaType);
Assert.NotSame(mediaType0.Parameters, mediaType1.Parameters);
Assert.Equal(mediaType0.Parameters.Count, mediaType1.Parameters.Count);
}
示例14: FileResult
/// <summary>
/// Creates a new <see cref="FileResult"/> instance with
/// the provided <paramref name="contentType"/>.
/// </summary>
/// <param name="contentType">The Content-Type header of the response.</param>
protected FileResult(MediaTypeHeaderValue contentType)
{
if (contentType == null)
{
throw new ArgumentNullException(nameof(contentType));
}
ContentType = contentType;
}
示例15: GetT_KnownTypeWithValidValue_Success
public void GetT_KnownTypeWithValidValue_Success()
{
var context = new DefaultHttpContext();
context.Request.Headers[HeaderNames.ContentType] = "text/plain";
var result = context.Request.GetTypedHeaders().Get<MediaTypeHeaderValue>(HeaderNames.ContentType);
var expected = new MediaTypeHeaderValue("text/plain");
Assert.Equal(expected, result);
}