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


C# HttpContext.EmitNotAcceptable方法代码示例

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


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

示例1: NegotiateFormat

        // Accept, Accept-Encoding flow
        internal IContentFormatter NegotiateFormat(HttpContext httpContext, string ext, ILightNodeOptions options, IOperationCoordinator coorinator)
        {
            var requestHeader = httpContext.Request.Headers;

            StringValues accepts;
            if (ForceUseFormatter != null) return ForceUseFormatter;
            if (!string.IsNullOrWhiteSpace(ext))
            {
                // Ext match -> ContentEncoding match
                var selectedFormatters = formatterByExt[ext] as ICollection<IContentFormatter> ?? formatterByExt[ext].ToArray();
                if (!selectedFormatters.Any())
                {
                    coorinator.OnProcessInterrupt(options, httpContext, InterruptReason.NegotiateFormatFailed, "Ext:" + ext);
                    options.Logger.NegotiateFormatFailed(OperationMissingKind.NegotiateFormatFailed, ext);
                    if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ThrowException)
                    {
                        throw new NegotiateFormatFailedException(OperationMissingKind.NegotiateFormatFailed, ext);
                    }
                    else
                    {
                        httpContext.EmitNotAcceptable();
                        if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ReturnErrorStatusCodeIncludeErrorDetails)
                        {
                            httpContext.EmitStringMessage("NegotiateFormat failed, ext:" + ext);
                        }
                    }
                    return null;
                }

                return SelectAcceptEncodingFormatter(requestHeader, selectedFormatters);
            }
            else if (requestHeader.TryGetValue("Accept", out accepts))
            {
                if (optionFormatters.Length == 1) return options.DefaultFormatter; // optimize path, defaultFormatter only

                // MediaType match -> ContentEncoding match
                var acceptsValues = GetDescendingQualityHeaderValues(accepts);
                var formatters = acceptsValues.SelectMany(x => formatterByMediaType[x.Item3]).ToArray();

                if (formatters.Length == 0)
                {
                    // only accept-encoding
                    goto CONTENT_ENCODING_MATCH;
                }

                return SelectAcceptEncodingFormatter(requestHeader, formatters);
            }

            // ContentEncoding match
            CONTENT_ENCODING_MATCH:
            {
                if (optionFormatters.Length == 1) return options.DefaultFormatter; // optimize path, defaultFormatter only

                // ContentEncoding match
                StringValues rawAcceptEncoding;
                if (!requestHeader.TryGetValue("Accept-Encoding", out rawAcceptEncoding))
                {
                    return options.DefaultFormatter;
                }

                var acceptEncodings = GetDescendingQualityHeaderValues(rawAcceptEncoding);
                var formatter = acceptEncodings
                    .Select(kvp => formatterByContentEncoding[kvp.Item3].FirstOrDefault())
                    .FirstOrDefault(x => x != null);

                if (formatter == null) return options.DefaultFormatter;
                return formatter;
            }
        }
开发者ID:neuecc,项目名称:LightNode,代码行数:70,代码来源:OperationHandler.cs


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