当前位置: 首页>>代码示例>>C#>>正文


C# Headers.MediaTypeHeaderValue类代码示例

本文整理汇总了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);
 }
开发者ID:tuespetre,项目名称:HttpAbstractions,代码行数:7,代码来源:MediaTypeHeaderValueTest.cs

示例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;
        }
开发者ID:genusP,项目名称:WebApi,代码行数:29,代码来源:ODataOutputFormatter.cs

示例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);
            }
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:27,代码来源:ContentResult.cs

示例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;
 }
开发者ID:migrap,项目名称:Migrap.AspNet.Http,代码行数:7,代码来源:HttpRequestExtensions.cs

示例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);
            }
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:40,代码来源:ViewExecutor.cs

示例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);
        }
开发者ID:volkanceylan,项目名称:Serenity,代码行数:34,代码来源:DynamicScriptMiddleware.cs

示例7: TwilioResponseResult

 public TwilioResponseResult(Action<TwilioResponse> buildResponse)
 {
     var response = new TwilioResponse();
     buildResponse(response);
     Content = response.Element.ToString();
     ContentType = new MediaTypeHeaderValue("text/xml");
 }
开发者ID:OlsonAndrewD,项目名称:the-phone-bible,代码行数:7,代码来源:TwilioResponseResult.cs

示例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;
            }
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:36,代码来源:OutputFormatter.cs

示例9: ValidateContentType

 private void ValidateContentType(MediaTypeHeaderValue contentType)
 {
     if (contentType.Type == "*" || contentType.SubType == "*")
     {
         throw new ArgumentException(string.Format(Resources.FormatterMappings_NotValidMediaType, contentType));
     }
 }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:7,代码来源:FormatterMappings.cs

示例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);
 }
开发者ID:tuespetre,项目名称:HttpAbstractions,代码行数:7,代码来源:MediaTypeHeaderValueTest.cs

示例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 };
        }
开发者ID:playgenhub,项目名称:rage-sga-server,代码行数:31,代码来源:HttpResponseHelper.cs

示例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);
        }
开发者ID:EgoDust,项目名称:HttpAbstractions,代码行数:8,代码来源:MediaTypeHeaderValueTest.cs

示例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);
 }
开发者ID:tuespetre,项目名称:HttpAbstractions,代码行数:9,代码来源:MediaTypeHeaderValueTest.cs

示例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;
        }
开发者ID:phinq19,项目名称:git_example,代码行数:14,代码来源:FileResult.cs

示例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);
        }
开发者ID:leloulight,项目名称:HttpAbstractions,代码行数:10,代码来源:HeaderDictionaryTypeExtensionsTest.cs


注:本文中的Microsoft.Net.Http.Headers.MediaTypeHeaderValue类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。