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


C# MediaTypeFormatter.SelectCharacterEncoding方法代码示例

本文整理汇总了C#中System.Net.Http.Formatting.MediaTypeFormatter.SelectCharacterEncoding方法的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeFormatter.SelectCharacterEncoding方法的具体用法?C# MediaTypeFormatter.SelectCharacterEncoding怎么用?C# MediaTypeFormatter.SelectCharacterEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.Formatting.MediaTypeFormatter的用法示例。


在下文中一共展示了MediaTypeFormatter.SelectCharacterEncoding方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SelectResponseCharacterEncoding

        /// <summary>
        /// Determine the best character encoding for writing the response. First we look
        /// for accept-charset headers and if not found then we try to match
        /// any charset encoding in the request (in case of PUT, POST, etc.)
        /// If no encoding is found then we use the default for the formatter.
        /// </summary>
        /// <returns>The <see cref="Encoding"/> determined to be the best match.</returns>
        protected virtual Encoding SelectResponseCharacterEncoding(HttpRequestMessage request, MediaTypeFormatter formatter)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }
            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            // If there are any SupportedEncodings then we pick an encoding
            if (formatter.SupportedEncodings.Count > 0)
            {
                // Sort Accept-Charset header values
                IEnumerable<StringWithQualityHeaderValue> sortedAcceptCharsetValues = SortStringWithQualityHeaderValuesByQFactor(request.Headers.AcceptCharset);

                // Check for match based on accept-charset headers
                foreach (StringWithQualityHeaderValue acceptCharset in sortedAcceptCharsetValues)
                {
                    foreach (Encoding encoding in formatter.SupportedEncodings)
                    {
                        if (encoding != null && acceptCharset.Quality != FormattingUtilities.NoMatch &&
                            (acceptCharset.Value.Equals(encoding.WebName, StringComparison.OrdinalIgnoreCase) ||
                            acceptCharset.Value.Equals("*", StringComparison.OrdinalIgnoreCase)))
                        {
                            return encoding;
                        }
                    }
                }

                // Check for match based on any request entity body
                return formatter.SelectCharacterEncoding(request.Content != null ? request.Content.Headers : null);
            }

            return null;
        }
开发者ID:chrisortman,项目名称:aspnetwebstack,代码行数:44,代码来源:DefaultContentNegotiator.cs

示例2: SelectResponseCharacterEncoding

        /// <summary>
        /// Determine the best character encoding for writing the response. First we look
        /// for accept-charset headers and if not found then we try to match
        /// any charset encoding in the request (in case of PUT, POST, etc.)
        /// If no encoding is found then we use the default for the formatter.
        /// </summary>
        /// <returns>The <see cref="Encoding"/> determined to be the best match.</returns>
        protected virtual Encoding SelectResponseCharacterEncoding(
            HttpRequestMessage request,
            MediaTypeFormatter formatter)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            // If there are any SupportedEncodings then we pick an encoding
            var supportedEncodings = formatter.SupportedEncodings.ToList();
            if (supportedEncodings.Count > 0)
            {
                // Sort Accept-Charset header values
                var sortedAcceptCharsetValues =
                    SortStringWithQualityHeaderValuesByQFactor(request.Headers.AcceptCharset);

                // Check for match based on accept-charset headers
                foreach (StringWithQualityHeaderValue acceptCharset in sortedAcceptCharsetValues)
                {
                    for (var i = 0; i < supportedEncodings.Count; i++)
                    {
                        var encoding = supportedEncodings[i];
                        if (encoding != null && acceptCharset.Quality != FormattingUtilities.NoMatch &&
                            (acceptCharset.Value.Equals(encoding.WebName, StringComparison.OrdinalIgnoreCase) ||
                            acceptCharset.Value.Equals("*", StringComparison.Ordinal)))
                        {
                            return encoding;
                        }
                    }
                }

                // Check for match based on any request entity body
                return formatter.SelectCharacterEncoding(request.Content != null ? request.Content.Headers : null);
            }

            return null;
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:50,代码来源:DefaultContentNegotiator.cs


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